如何获取 django csrf 失败的详细日志?

How can I get a detailed log of django csrf failure?

我正在对 Django 应用程序进行故障排除。

最近该应用程序似乎随机 生成 CSRF 验证错误:

CSRF verification failed. Request aborted. (导致 403)

在哪里可以找到验证失败原因的详细信息?

您看到的错误是在客户端 - 默认情况下它不会知道您的服务器出了什么问题,除非您设置了 DEBUG = True(您不想在生产中这样做) ).

如果它可以在您的登台服务器上复制,那么修复起来很容易,因为您可以在登台上使用 DEBUG = True 复制错误,并快速查看 Django 的 csrf.py 中验证失败的位置。

您要查找的是 which of these error is occurring on your server.

的输出

如果您 implement logging in Django you'll be able to investigate and determine which of these errors was triggered on your production site. A service like Sentry 让这更简单,因为它会在发生错误时随时向您发送回溯。