django, CSRF token missing or incorrect

CSRF token missing or incorrect

--

 

1 在 templete 中, 为每个 POST form 增加一个 {% csrf_token %} tag. 如下:

 

<form>

    {% csrf_token %}

</form>

 

2 在 view 中, 使用 django.template.RequestContext 而不是 Context.

 

render_to_response, 默认使用 Context. 需要改成 RequestContext.

 

导入 class:

 

from django.template import RequestContext

 

给 render_to_response 增加一个参数:

 

def your_view(request):

    ...

    return render_to_response('template.html',

          your_data,

          context_instance=RequestContext(request)

    )
原文地址:https://www.cnblogs.com/qq78292959/p/3230579.html