Django CSRF 验证

发布于 2024-11-18 21:26:53 字数 865 浏览 3 评论 0原文

我无法解决 CSRF 令牌验证 403 错误。 这是我所拥有的:

视图:

from django.template import RequestContext
def add(request, name):
#logic
return render_to_response("category/add.html", {"form": form}, 
    context_instance = RequestContext(request))    

模板:

<form action="/category/add/" method="post">
    {% csrf_token %}
    New Category: <input type="text" name="name" id="categoryname" />
    <input type="submit" value="Hinzufuegen" />
</form>

设置:

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',
)

I can't get around the CSRF token validation 403 error.
Here's what I have:

view:

from django.template import RequestContext
def add(request, name):
#logic
return render_to_response("category/add.html", {"form": form}, 
    context_instance = RequestContext(request))    

template:

<form action="/category/add/" method="post">
    {% csrf_token %}
    New Category: <input type="text" name="name" id="categoryname" />
    <input type="submit" value="Hinzufuegen" />
</form>

settings:

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',
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凡间太子 2024-11-25 21:26:53

您是否将 POST 数据传递到表单进行验证?你必须做这样的事情:

     from django.template import RequestContext
     def add(request, name):
       # Logic
       if request.type == "POST":
         form = YourFormClass(request.POST)
         if form.is_valid():
             # Success!
         else:
             # Invalid form
       else:
         return render_to_response("category/add.html", {"form": form}, 
         context_instance = RequestContext(request))

Are you passing the POST data to the form for validation? You have to do something like this:

     from django.template import RequestContext
     def add(request, name):
       # Logic
       if request.type == "POST":
         form = YourFormClass(request.POST)
         if form.is_valid():
             # Success!
         else:
             # Invalid form
       else:
         return render_to_response("category/add.html", {"form": form}, 
         context_instance = RequestContext(request))
初心未许 2024-11-25 21:26:53

似乎您需要添加

'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',

到您的 MIDDLEWARE_CLASSES 中。似乎它没有验证 CSRF 令牌,只是生成......
帮助我我的案例 ...

Seems like you need to add

'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',

to your MIDDLEWARE_CLASSES. Seems that it is not validating CSRF token, just generating...
helped me in my case...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文