python config_error ai_generated true

django.core.exceptions.ImproperlyConfigured: 未设置CSRF cookie。请求失败。

django.core.exceptions.ImproperlyConfigured: CSRF cookie not set. Request failed.

ID: python/django-missing-request-csrf-token

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-03-15首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

视图未使用@csrf_exempt,但模板中的表单缺少{% csrf_token %},导致CSRF中间件拒绝POST请求。

English

The view is not using @csrf_exempt but the template lacks {% csrf_token %} in the form, causing CSRF middleware to reject the POST request.

generic

解决方案

  1. 95% 成功率
    Ensure the form in template includes {% csrf_token %} inside <form> tag.
  2. 90% 成功率
    If using AJAX, include 'X-CSRFToken' header with token from cookie.

无效尝试

常见但无效的做法:

  1. 95% 失败

    Disables security protection for all views, causing security vulnerabilities and potential 403 errors elsewhere.

  2. 90% 失败

    Overrides CSRF protection globally, making the app vulnerable to CSRF attacks and breaking expected behavior.