Search code examples
pythondjangodjango-viewsdjango-csrfcsrf-protection

csrf_token not created on pages loaded via post


I have an issue with the {% csrf_token %} template tag in Django. On pages loaded via get it is fine but if I use post to load a page the tag is not loaded to forms on the page requested with post.

I am using render_to_response to render the pages

Any ideas?

Thanks


Solution

  • Are you updating your context with the csrf token when calling render_to_response from a POST request? Like so:

    from django.core.context_processors import csrf
    from django.shortcuts import render_to_response
    
    def my_view(request):
        if request.method == 'POST':
            c = {}
            c.update(csrf(request))
            return render_to_response("a_template.html", c)
        else:
            # GET code...