开发者

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

开发者 https://www.devze.com 2023-01-01 03:55 出处:网络
I have found myself writing the same view over and over.It is basically this: def home_index(request):

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 开发者_如何学JAVAempty context. So how can I use a generic view and still get the power of RequestContext?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号