CSRF令牌丢失或不正确,即使包含令牌标签也是如此 [英] CSRF token missing or incorrect, even after including the token tag

查看:75
本文介绍了CSRF令牌丢失或不正确,即使包含令牌标签也是如此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到 CSRF令牌丢失或不正确错误,但是我已经在网络表单中添加了 {%csrf_token%} 标签。知道为什么我仍然会遇到此错误吗?

I am getting a 'CSRF token missing or incorrect' error, but I have already added the {% csrf_token%} tag in the web form. Any idea why I still face this error?

def index(request):
    if request.method == 'POST':
        form = RequestForm(request.POST)
        if form.is_valid():
            form.save()
            return render(request, 'index.html')
    else:
        form = RequestForm()
    return render(request, 'index.html', {'form': form})`

我无法发布模板,但是我使用如下行所示的令牌

I cannot post the template, but I use the token as shown in line below

<form id="reqForm" action="" method="POST" enctype="text/plain">{% csrf_token %}


推荐答案

您的问题似乎是表格中的 enctype = text / plain 。 CSRF保护假定帖子数据是经过表单编码的。

It looks like your problem is enctype="text/plain" in your form. The CSRF protection assumes that the post data is form encoded.

最简单的解决方案是将其完全删除,等效于:

The simplest fix is to remove that entirely, which is equivalent to:

enctype="enctype=application/x-www-form-urlencoded"

如果您要上传表单中的文件,则可以使用:

If you were uploading files in your form you would use:

enctype="multipart/form-data"

这篇关于CSRF令牌丢失或不正确,即使包含令牌标签也是如此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆