python auth_error ai_generated true

Forbidden (CSRF token missing or incorrect.) at /login/

ID: python/django-csrf-token-missing

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-04-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Django's CSRF protection requires a valid CSRF token in POST requests; missing or invalid token causes 403 error.

generic

中文

Django的CSRF保护要求在POST请求中包含有效的CSRF令牌;缺失或无效令牌导致403错误。

Workarounds

  1. 95% success Include CSRF token in template form
    In template: {% csrf_token %} inside the <form> tags.
  2. 85% success Use @csrf_exempt decorator on specific view
    from django.views.decorators.csrf import csrf_exempt
    @csrf_exempt
    def my_view(request): ...

Dead Ends

Common approaches that don't work:

  1. Disabling CSRF globally 80% fail

    Security risk; not recommended.

  2. Adding @csrf_exempt to all views 70% fail

    Weakens security; may not be appropriate.