Search code examples
djangocsrfdjango-csrf

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


I am troubleshooting a Django app.

Recently the app seems to Randomly generate CSRF verification errors:

CSRF verification failed. Request aborted. (Resulting in a 403)

Where can I find detailed information on the cause of the verification failure?


Solution

  • The error you're seeing is on the client side - which won't by default know what's going wrong on your server unless you have set DEBUG = True (which you don't want to do in production).

    If it was replicable on your staging server it would be easy to fix, since you could replicate the error with DEBUG = True on staging and quickly see where the verification fails in Django's csrf.py.

    What you're looking for is the output of which of these error is occurring on your server.

    If you 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 makes this even simpler since it will send you the traceback anytime an error happens.