python
auth_error
ai_generated
true
禁止访问(CSRF令牌缺失或不正确)在/login/
Forbidden (CSRF token missing or incorrect.) at /login/
ID: python/django-csrf-token-missing
80%修复率
87%置信度
0证据数
2024-04-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
Django的CSRF保护要求在POST请求中包含有效的CSRF令牌;缺失或无效令牌导致403错误。
English
Django's CSRF protection requires a valid CSRF token in POST requests; missing or invalid token causes 403 error.
解决方案
-
95% 成功率 Include CSRF token in template form
In template: {% csrf_token %} inside the <form> tags. -
85% 成功率 Use @csrf_exempt decorator on specific view
from django.views.decorators.csrf import csrf_exempt @csrf_exempt def my_view(request): ...
无效尝试
常见但无效的做法:
-
Disabling CSRF globally
80% 失败
Security risk; not recommended.
-
Adding @csrf_exempt to all views
70% 失败
Weakens security; may not be appropriate.