﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
12815	Add a lazy TemplateResponse	Russell Keith-Magee	nobody	"Proposal from Simon Willison: 

Summary: Add a TemplateResponse class - a response that contains a template and context, but doesn't evaluate the template until it is required (or explicitly 'baked'). This allows middlewares, decorators, or view wrappers to change the template.
----
The original proposal comes from [http://groups.google.com/group/django-developers/msg/b1b3f8854b9ae2b1 this django-users thread], in the context of making CSRF easier to use. Simon said:

I've been experimenting recently with  a TemplateResponse class which lets you do this: 
{{{
def view(request): 
    return TemplateResponse(request, 'my_template.html', {'foo': 'bar'}) 
}}}

It's an HttpResponse class that's designed to be lazily evaluated.  This makes it really useful for subclassing - you can do things like  this: 
{{{
class MyGenericView(object): 
   def view(self, request): 
       # Do something complicated 
       return TemplateResponse(request, 'my_template.html', {'foo': 'bar'}) 
class MyCustomisedView(object): 
   def view(self, request): 
       response = super(MyCustomisedView, self).view(request) 
       response.context['foo'] = 'baz' 
       if is_mobile_phone(request): 
           response.template = 'my_mobile_template.html' 
       return response 
}}}
This pattern would be particularly valuable for customising the admin,  which currently uses nasty extra_context argument hacks to achieve  something that isn't nearly as useful. 

My current implementation of TemplateResponse is [http://github.com/simonw/django-openid/blob/master/django_openid/response.py here] - there's a SimpleTemplateResponse as well which doesn't use RequestContext (but has a more verbose name to discourage its use). 
"		closed	Core (Other)			fixed		Carl Meyer	Design decision needed	1	0	1	0	0	0
