1

My Django app sends emails correctly to all the ADMINS as long as I don't have a custom error handler.

However, when I add a handler500 to show my custom 500_error.html page, no emails are sent. The 500_error.html page is shown properly. It seems that when you have a handler Django assumes that you take care of the error and thus does not mail anything. (Does this make any sense?)

My questions are:

  1. How can I still force Django to send emails even when the handler500 is used?, or

  2. How can I get the error description and mail it within my handler500 view function using send_email?

Thanks!

marcos
  • 93
  • 8
  • can you provide the code of your error handler? – bb4L Jul 03 '20 at 15:51
  • `def handler500(request):` `t = loader.get_template('500_error.html')` `return HttpResponseServerError(t.render(Context({'request': request,})))` – marcos Jul 03 '20 at 19:17

2 Answers2

1

If all you want is to show a customized error page, then no handler is required.

Django will search for a template named 500.html in the root of your templates folder; if available it will be shown. If DEBUG = False, an email will be sent.

See the documentation:

If a view results in an exception, Django will, by default, call the view django.views.defaults.server_error, which either produces a “Server Error” message or loads and renders the template 500.html if you created it in your root template directory.

See also Django, creating a custom 500/404 error page

SaeX
  • 17,240
  • 16
  • 77
  • 97
0

Ok, the issue was that the function called by handler500 needs to return a 500 status code for Django to sed the email.

Setting this handler sends the email to the Admins and also shows the custom template:

def handler500(request):
    t = loader.get_template('500_error.html')
    response = HttpResponseServerError(t.render())
    response.status_code = 500
    return response
marcos
  • 93
  • 8