【发布时间】:2012-09-25 17:27:43
【问题描述】:
所以我得到了 Forbidden (403) CSRF 验证失败。请求中止。失败原因:CSRF 令牌丢失或不正确。
我的中间件类中有“django.middleware.csrf.CsrfViewMiddleware”。
这是我的模板
<form name="input" action="/login/" method="Post"> {% csrf_token %}
<input type="submit" value="Submit"></form>
这是我的看法
from django.shortcuts import render_to_response
from django.core.context_processors import csrf
from django.template import RequestContext
def login(request):
csrfContext = RequestContext(request)
return render_to_response('foo.html', csrfContext)
嗯,我是 Django 和大多数 Web 开发的新手,但我似乎无法在这里找到问题。任何帮助将不胜感激!
我也试过django文档中的方法
c = {}
c.update(csrf(request))
# ... view code here
return render_to_response("a_template.html", c)
【问题讨论】:
-
附注:如果您是 Django 新手,我会考虑使用基于类的视图来处理几乎所有内容。即使您只是覆盖
post,它们至少会强制服务器将响应哪些 HTTP 请求类型。 -
创建
-
您需要在模板中添加
{% csrf_token %}。并确保RequestContext存在.. 当然,settings.py中的相应中间件和.. 如果问题仍然存在。我不确定,但是.. 试试@csrf_exemptdecorators.. 等等