Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get direct_to_template to pass RequestContext in django?

I have found myself writing the same view over and over. It is basically this:

def home_index(request):
    return render_to_response('home/index.html', RequestContext(request))

To keep with the dry principal, I would like to utilize a generic view. I have seen direct_to_template, but it passes an empty context. So how can I use a generic view and still get the power of RequestContext?

like image 277
Jason Webb Avatar asked Dec 29 '22 16:12

Jason Webb


1 Answers

direct_to_template, like all generic views, already uses a RequestContext, so you don't need to do anything else to enable it.

However I'm not sure if what you're really asking is whether you can pass additional context items - and you can, by using the extra_context dictionary parameter, either in the URLconf or in a wrapper view.

Also you should ask yourself why you're creating multiple views that simply render templates. If that's what you are mostly doing, you may find that Django's built-in flatpages app is better than hard-coding your views.

like image 199
Daniel Roseman Avatar answered Apr 26 '23 09:04

Daniel Roseman