This post is the sixth in a series on Django tricks.
We started with automating Admin Model creation, and continued with automating initialization and installation, model mixins, forms and app settings.
This post diverges from automation to point out some useful view utilities.
Render Response
This trick is more like standard Django practice: wrap render_to_response with your own function that uses your own RequestContext.
def render_response(request, template, dictionary=None): """ Return render_to_response with context_instance=RequestContext(request). """ dictionary = dictionary or {} return render_to_response(template, dictionary, context_instance=RequestContext(request)) def render_string(request, template, dictionary=None): dictionary = dictionary or {} return render_to_string(template, dictionary, context_instance=RequestContext(request))
One small thing sometimes forgotten is that when you call render_response from your view, you don’t have to write out the dictionary, which is really just a copy/double-paste of local variable names. Instead, call locals().
some_view.py
def info(request): user_count = User.objects.count() visitor_count = Visitor.objects.count() pledge_count = ... donation_count = RecipientPayment.objects.count() return render_response(request, 'procrasdocoder/info.html', locals())
Extract Parameters
Another useful convenience function is extract_parameters, which extracts expected and optional parameters from a request’s POST and GET objects, as well as reporting appropriate errors.
Here’s an example use:
def receive_data(request): expected_parameters = ["private_key", "prefs"] optional_parameters = ["foo", "bar"] response = extract_parameters(request, "POST", expected_parameters) if not response['success']: Log.Error(response['reason'], "communication_error") return json_failure(message) parameters = response['parameters'] private_key = parameters['private_key'] ...
Here is the definition for extract_parameters:
def extract_parameters(request, method_type, expected_parameters, optional_parameters=None): ret = {} if not getattr(request, method_type): return {'success': False, 'reason': "Expected %s parameters" % method_type} for p in expected_parameters: try: v = getattr(request, method_type).get(p, None) if v == None: return {'success': False, 'reason': "Missing expected parameter: %s" % p} ret[p] = v except: return {'success': False, 'reason': "Something unexpected happened while extracting parameters"} optional_parameters = optional_parameters or {} for p in optional_parameters: try: v = getattr(request, method_type).get(p, None) if v: ret[p] = v except: return {'success': False, 'reason': "Something unexpected happened while extracting optional parameters"} return {'success': True, 'parameters': ret}
This is a marathon super-bowl blogging experience for me, and the English to Code ratio is quickly approaching zero. I’ve left the best for last, though, so check it out: Part 7 – Django from shell.
Proudly ProcrasDonating,
Lucy.








