I tried of writing those vars and stuck them back into the render function of Django shortcut which means write at least 4 times for just one var.
For instance:
def foo(request):
bar = request.GET.get('bar')
bar += 1
result = {'bar':bar}
render('parrot.html', result)
It’s OK when I write just one var, but when comes to 20 more GET vars, that’s totally a disaster.
Any suggestion?
First, the type of the return value of
request.GET.get('bar')is eitherstrorNoneTypeinstead ofint, thus you can’t simply add it by1.Second, remember there is ‘no magic’, I’d like to rewrite the code like the following, but there are still four occurrences of
barIf you don’t like this sort of verbosity, just abstract the
try...exceptpart to a helper such asget_int. When there are 20 or more vars to operate on, you may need higher level of abstraction such as Django forms…