4

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?

Jason Webb
  • 7,528
  • 9
  • 38
  • 49

2 Answers2

9

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.

Daniel Roseman
  • 541,889
  • 55
  • 754
  • 786
  • Turns out I had a syntax error, so thanks for clarifying that `RequestContext` should be there since that made me look at the template again. And your right, it looks like I should use flatpages. Appreciate the info and help. – Jason Webb May 22 '10 at 14:41
1

I remember having the same problem, and writing something like this, but looking at the direct_to_template code it seems in new versions of django this problem doesn't exist anymore. direct_to_template passes the correct context.

Ofri Raviv
  • 22,321
  • 3
  • 51
  • 54