python auth_error ai_generated true

BadRequest: The CSRF token is missing.

ID: python/flask-form-csrf-token-missing

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

Flask-WTF CSRFProtect is enabled but the HTML form lacks {{ csrf_token() }} or the AJAX request omits the X-CSRFToken header.

generic

中文

启用了 Flask-WTF CSRFProtect,但 HTML 表单缺少 {{ csrf_token() }} 或 AJAX 请求缺少 X-CSRFToken 头。

Workarounds

  1. 93% success
    In template: <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
  2. 90% success
    In AJAX: headers: {'X-CSRFToken': getCookie('csrf_token')}

Dead Ends

Common approaches that don't work:

  1. 30% fail

    Disables CSRF protection entirely; security risk.

  2. 70% fail

    Flask-WTF looks for form field or header, not JSON.