django出错:CSRF token missing or incorrect.

解决方案:

1.在对应的模板中, 为每个POST的form添加一行代码:{%
csrf_token %}。 如下所示:

<form action = "" method = "post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type = "submit" value = "Submit">
</form>

2.在对应的views.py中, 使用 django.template.RequestContext 而不是
Context。因为render_to_response 默认使用的是Context,我们需要改成 RequestContext。给
render_to_response函数增加一个参数,如下所示:

return render_to_response(‘contact_form.html‘,
{‘form‘ : form},context_instance = RequestContext(request))

3.修改settings.py,在MIDDLEWARE_CLASSES中加上‘django.middleware.csrf.CsrfViewMiddleware‘。如下所示:


1 MIDDLEWARE_CLASSES = (
2 ‘django.contrib.sessions.middleware.SessionMiddleware‘,
3 ‘django.middleware.common.CommonMiddleware‘,
4 ‘django.middleware.csrf.CsrfViewMiddleware‘,
5 ‘django.contrib.auth.middleware.AuthenticationMiddleware‘,
6 ‘django.contrib.messages.middleware.MessageMiddleware‘,
7 ‘django.middleware.clickjacking.XFrameOptionsMiddleware‘,
8 )

时间: 2024-10-14 14:39:42

django出错:CSRF token missing or incorrect.的相关文章

解决django出现CSRF token missing or incorrect.错误

给template中的每个POST form添加{% csrf_token %}标签. <form action="/contact/" method="post">     {% csrf_token %}     <p>Subject: <input type="text" name="subject"></p>     <p>Your e-mail (optio

&quot;CSRF token missing or incorrect.&quot;的解决方法.

现象: Forbidden (403)CSRF verification failed. Request aborted.HelpReason given for failure:CSRF token missing or incorrect. In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been use

关于 DjangoUeditor 上传图片图片失败,csrf token missing or incorrect 的解决办法

Forbidden (CSRF token missing or incorrect.): /ueditor/controller/ [27/Jun/2017 23:49:25] "POST /ueditor/controller/?imagePathFormat=courses%2Fueditor%2F&filePathFormat=courses%2Fueditor%2F&action=uploadvideo&encode=utf-8 HTTP/1.1" 4

Forbidden (CSRF token missing or incorrect.):错误解决办法

在JS中,使用post方法提交数据到后台,出现错误: Forbidden (CSRF token missing or incorrect.):.........; 解决办法: 在页面导入JS的位置,补上以下代码即可: 1 <script> 2 $.ajaxSetup({ 3 data:{csrfmiddlewaretoken:'{{ csrf_token }}'} 4 }) 5 </script> 原文地址:https://www.cnblogs.com/lhb-alan/p/1

Forbidden (CSRF token missing or incorrect.):

CSRF令牌失效或丢失,Ajax请求页面报错(403 Forbidden ) csrftoken存在 页面响应为CSRF验证失败请求被中断,经过测试,该错误并非是没有在表单中加入{% csrf_token%} 导致[试过] 刷新dns缓存依然无法解决该问题, 解决: 检查setting配置中 'django.middleware.csrf.CsrfViewMiddleware', 在浏览器隐私中清除cookie,[无效] 刷新dns[无效] 最后解决方案: 在引用js之前加入Ajax启动项,之前

django rest framework csrf failed csrf token missing or incorrect

REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework.authentication.TokenAuthentication',)} or MIDDLEWARE_CLASSES = ( 'store.disable.DisableCSRF',) disable.py class DisableCSRF(object): def process_request(self, request):        seta

djangopost请求报错:Forbidden (CSRF token missing or incorrect.)

解决办法:引入csrf_exempt包: from django.views.decorators.csrf import csrf_exempt 然后增加装饰器: @csrf_exempt def register(request): request.encoding='utf-8' if request.GET: return render(request,'register.html') elif request.POST: dictData = request.POST context

ajax $.post 一直报 Forbidden (CSRF token missing or incorrect.)

由于后台整合类视图代码,所以修改了写法,完了之后用下面的写法写的post请求都报 403 error $.post( "{% url 'test_record:select_node_page' ret.uut_id %}", { // uut_id: "{{ ret.uut_id }}", filter: JSON.stringify(filters), }, function(data){ var retData = JSON.parse(data); cons

Django后台post请求中的csrf token

使用Requests库操作自己的Django站点,post登陆admin页面返回403,serverlog显示csrf token not set. csrf token是get登陆页面时服务器放在cookies中返回的. 所以完整的登陆应该是这样的: import requests s = requests.Session() r = s.get("http://serveraddr/admin") s.cookies = r.cookies loginpage = s.post(a