Django CSRF 验证失败.请求中止.- CSRF cookie 未设置 [英] Django CSRF verification failed. Request aborted.- CSRF cookie not set
问题描述
我知道以前有人问过这个问题.我已经尝试了人们给出的几乎所有选项,但我似乎无法解决它.我是一个完整的新手,所以请让我知道我哪里出错了.
我正在尝试编写一个简单的原始表单.到目前为止,我还没有实现任何身份验证或会话机制(但从我所读到的内容来看,这与此问题无关.如果我错了,请纠正我).
当我尝试提交表单时,出现以下错误:
禁止 (403)CSRF 验证失败.请求中止.给出的失败原因:未设置 CSRF cookie.
这是我的代码:
我的 Views.py 有这个方法:
def 提交(请求):全局字母数组菜肴 = Dish.objects.all().order_by('name')如果 request.method == "POST":打印请求.POST['餐厅']打印请求.POST['评级']render_to_response('index.html', {}, context_instance=RequestContext(request))别的:render_to_response('index.html', {}, context_instance=RequestContext(request))
许多人说使用 RequestContext 解决了这个问题,但对我来说,即使这样也行不通.
模板如下:
<div class="form-group"><label for="">评分</label><select class="form-control" name="rating"><option>--</option><option>1(非常糟糕)</option><option>2(坏)</option><option>3(平均)</option><option>4(好)</option><option>5(优秀)</option></选择>
<button type="submit" class="btn btn-primary btn-block"><i class="fa fa-check-circle"></i>保存按钮></表单>
settings.py 中的 middleware_classes 看起来像:
MIDDLEWARE_CLASSES = ('django.middleware.common.CommonMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware',)
django.middleware.csrf.CsrfViewMiddleware 在那里,它在 'django.contrib.sessions.middleware.SessionMiddleware' 下方
我的 url.py 有条目:
url(r'^admin/', include(admin.site.urls)),url(r'^index/$', 'testapp.views.index'),url(r'^starts_with/(?P.+)/dish/(?Pd+)/$', 'testapp.views.alphabet_dish'),url(r'^starts_with/(?P.+)/$', 'testapp.views.alphabet'),url(r'^submit/$', 'testapp.views.submit'),
我真的不确定这里有什么问题.正如我所说,我在这里阅读了类似的帖子,并尝试了回复中提到的所有内容.我错过了什么?我的浏览器是 Chrome,它接受 cookie.
您的 CSRF 设置很好.问题是您没有返回结果.请记住,视图是一个函数.你需要 return render_to_response(...)
而不仅仅是调用它(这也是为什么通过删除 CSRF 你得到了 did not return an HttpResponse
错误)
除此之外,您正在做一些 Django 错误的常规操作:
因此:
def 提交(请求):# 全局字母数组菜肴 = Dish.objects.all().order_by('name')如果 request.method == "POST":打印请求.POST['餐厅']打印请求.POST['评级']返回渲染(请求,'index.html',{})
I know this question has been asked before. I have tried almost all the options given by people but I cannot seem to solve it. I am a complete newbie so please let me know where I am going wrong.
I am trying to write a simple raw form. I have not implemented any authentication or session mechanism until now (but from what I have read that does not matter to this problem. Correct me if I am wrong).
When I try to submit my form, I get this error:
Forbidden (403)
CSRF verification failed. Request aborted.
Reason given for failure:
CSRF cookie not set.
This is my code:
My Views.py has this method:
def submit(request):
global alphabet_array
dishes = Dish.objects.all().order_by('name')
if request.method == "POST":
print request.POST['restaurant']
print request.POST['rating']
render_to_response('index.html', {}, context_instance=RequestContext(request))
else:
render_to_response('index.html', {}, context_instance=RequestContext(request))
Many have said that using RequestContext solves this issue but for me even that is not working.
The template looks as below:
<form role="form" action="/submit/" method="post">{% csrf_token %}
<div class="form-group">
<label for="">Restaurant Name</label>
<input type="text" name="restaurant" class="form-control" id="">
</div>
<div class="form-group">
<label for="">Rating</label>
<select class="form-control" name="rating">
<option>--</option>
<option>1 (very bad)</option>
<option>2 (bad)</option>
<option>3 (average)</option>
<option>4 (good)</option>
<option>5 (excellent)</option>
</select>
</div>
<button type="submit" class="btn btn-primary btn-block"><i class="fa fa-check-circle"></i> Save</button>
</form>
The middleware_classes in settings.py looks like:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
django.middleware.csrf.CsrfViewMiddleware is there and it is below 'django.contrib.sessions.middleware.SessionMiddleware'
My url.py is has entries:
url(r'^admin/', include(admin.site.urls)),
url(r'^index/$', 'testapp.views.index'),
url(r'^starts_with/(?P<alphabet>.+)/dish/(?P<dish_id>d+)/$', 'testapp.views.alphabet_dish'),
url(r'^starts_with/(?P<alphabet>.+)/$', 'testapp.views.alphabet'),
url(r'^submit/$', 'testapp.views.submit'),
I am really not sure what is the problem here. As I said, I have read similar posts here and tried everything mentioned in the responses. What have I missed? My browser is Chrome and it is accepting cookies.
Your CSRF setup is fine. The problem is you are not returning the result. Remember that the view is a function. You need to return render_to_response(...)
not just call it (which is also why by removing CSRF you got the didn't return an HttpResponse
error)
Other than that, you are doing a few general things that are django-ically wrong:
Hence:
def submit(request):
# global alphabet_array
dishes = Dish.objects.all().order_by('name')
if request.method == "POST":
print request.POST['restaurant']
print request.POST['rating']
return render(request, 'index.html', {})
这篇关于Django CSRF 验证失败.请求中止.- CSRF cookie 未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!