python - GET 表单方法上的 CSRF 验证错误

标签 python django django-forms django-csrf

我收到错误 CSRF 验证失败。尝试提交表单时,请求已中止。。我已经使用了 GET 方法,但出现错误。为了确定起见,我还添加了 {% csrf_token %} token ,但错误仍然存​​在。

示例 HTML:

<body>
    <form action="/entry/" method="get" name="Form1"><br>
        {% csrf_token %}
        <select name="Date" size="1">
            <option>30</option>
        </select>
        &nbsp;
        <select name="Month">
            <option>09</option>
        </select>
        <select name="Year">
            <option>2015</option>
        </select>
        <input id="Save" style="height: 50px; width: 100px;" type="submit" value="Save">
    </form>
</body>

views.py 文件

def getuser(request):
    return render(request, 'index.html')
    
def putrecord(request):
    date = request.GET['Date']
    print date
    month = request.GET['Month']
    year = request.GET['Year']
    time_stamp = date + '/' + month + '/' + year
    print time_stamp
    return render(request, 'index.html', {})
    

url.py

urlpatterns = patterns('',

    url(r'^expenseapp/', getuser),
    url(r'^entry/', putrecord),
)

我的设置文件中还有CsrfViewMiddleware。我怎样才能摆脱这个错误?

最佳答案

尝试从浏览器中删除cookie,CSRF使用cookie来存储 token 。

关于python - GET 表单方法上的 CSRF 验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32920532/

相关文章:

python - 使用 sleep() 时高效快速的 Python While 循环

python - Django:无法解析相关模型 'users.UserProfile'

python - Django Rest框架中多对多中间模型的实现方法

django 表单给出 : Select a valid choice. 该选项不是可用选项之一

python - Django 表单字段 : Styling for Bootstrap without losing functionality?

python - 获取值错误 : "Can only tuple-index with a MultiIndex "

python - 使用转换键的类似字典的类

django - 如何在Django中的模型上使用error_messages

python - 在 Python 3 中转换为不同的位置符号

python - Django 子查询错误