Search code examples
djangocsrfrequestcontext

Obtaining a RequestContext for a templatetag included on every page


I have a templatetag which generates a form, this therefore requires the {% csrf_token %} for security, which in turn requires a RequestContext object instead of the standard Context object.

Now, the particular templatetag/form in question is included in the header of every page. Currently every page doesn't render_to_response with a RequestContext object.

Is there any way in which I can get around this issue without having to changing every view within the entire site to pass through the RequestContext?

Thanks in advance.


Solution

  • All though there may be some workarounds, the best practice is definitely to add the RequestContext everywhere. This has advantages for other middlewares as well.

    But there is also a legacy method, that will be removed in Django 1.4 that does not require you to change all your views. I would recommend against it unless you don't have much option.

    http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#legacy-method