python config_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-03-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 95% fail

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

  2. 90% fail

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