Login

another render_to_response wrapper

Author:
ro60
Posted:
December 29, 2007
Language:
Python
Version:
.96
Tags:
Score:
0 (after 0 ratings)

Wrapper for render_to_response that allows you to pass in a optional preinitialized HttpResponse object. Helpful when you want to want to set cookies or just add some extra initialization to your HttpResponse. If no HttpResponse is passed in the normal render_to_response is called. It's called exactly like the normal render_to_response except that you can pass in a kwargs response pair if you wish.

Like so:

code render_response('index.html',{'aparam': val}, context_instance=RequestContext(request),response=my_response)

1
2
3
4
5
6
7
8
9
def render_response(*args, **kwargs):
        response = kwargs.pop('response', None)
        if response == None:
            from django.shortcuts import render_to_response
            return render_to_response(*args, **kwargs)
        else:
            from django.template import loader
            response.content = loader.render_to_string(*args, **kwargs)
            return response

More like this

  1. render_to_response wrapper by Magus 8 years, 4 months ago
  2. Resource by zvoase 6 years, 9 months ago
  3. DRY template rendering decorator update by jakecr 5 years, 4 months ago
  4. Django JSONP Decorator by cominatchu 4 years, 9 months ago
  5. 401 HttpResponse by David 7 years ago

Comments

Please login first before commenting.