Is there a way to force direct_to_template to pass the RequestContext to django?

I wrote the same look over and over again. Mainly:

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

To keep the directive dry, I would like to use a general view . I saw direct_to_template , but it passes an empty context. So, how can I use the general view and still receive RequestContext authority?

+4
source share
2 answers

direct_to_template , like all common views, already uses RequestContext, so you don’t have to do anything to enable it.

However, I'm not sure if you are really asking if you can pass extra context items - and you can, using the extra_context dictionary extra_context , either in the URLconf or in the shell view.

You should also ask yourself why you are creating several views that simply display templates. If this is what you mostly do, you may find that Django's built-in flatpages application is better than hard-coding your views.

+9
source

I remember that I had the same problem and wrote something like this, but looking at the direct_to_template code , it seems that in new versions of django this problem no longer exists. direct_to_template passes the correct context.

+1
source

Source: https://habr.com/ru/post/1310585/


All Articles