problems with csrf_token

前端 未结 2 521
一整个雨季
一整个雨季 2021-01-22 18:31

I\'m doing wiki in django tutorial and in tutorial earlier version of django is used where csrf token is not compulsory. I\'ve added {% csrf_token %} to the form, however I\'m n

相关标签:
2条回答
  • 2021-01-22 18:48

    You haven't quite understood step 3 here: you need to use RequestContext to ensure that the context processor is run.

    return render_to_response("edit.html", {"page_name" : page_name,
                                            "content" : content},
                               context_instance=RequestContext(request))
    

    With that, you don't need the c.update(csrf(request)) bit.

    0 讨论(0)
  • 2021-01-22 19:02

    You don't need to do anything more.

    Just place {% csrf_token %} into your form and send it via POST.

    All other stuff will be done inside django.middleware.csrf.CsrfViewMiddleware.

    You should enable it in your settings.py (if you haven't done this):

    MIDDLEWARE_CLASSES += (`django.middleware.csrf.CsrfViewMiddleware`,)
    TEMPLATE_CONTEXT_PROCESSORS += (`django.core.context_processors.csrf`,)
    

    Last line is not required if you use RequestContext (not a simple Context) in your templates (Note: render_to_response() uses Context when direct_to_template() uses RequestContext)

    0 讨论(0)
提交回复
热议问题