Search code examples
djangoformscsrf

Django CSRF on 500 error page with a form


I got a 500 error page which has a form, however the CSRF token is not generated when 500 error page is thrown. What's the best way to generate the CSRF token on a 500 error page to make the form post work? Should I just define my own custom 500 error view?


Solution

  • OK, I tried defining the custom 500 view and worked, for anyone who has the same issue, here it's how:

    define a custom 500 error view

    from django.shortcuts import render
    
    def server_error(request):
        vars = {}
        return render(request, '500.html', vars, status=500)
    

    then in the main urls.py add:

    handler500 = 'your_app.views.errors.server_error'