Take the 2-minute tour ×

I'm running Django 1.0 and I'm close to deploying my app. As such, I'll be changing the DEBUG setting to False.

With that being said, I'd still like to include the stacktrace on my 500.html page when errors occur. By doing so, users can copy-and-paste the errors and easily email them to the developers.

Any thoughts on how best to approach this issue?

share|improve this question
add comment

4 Answers

up vote 13 down vote accepted

Automatically log your 500s, that way:

  • You know when they occur.
  • You don't need to rely on users sending you stacktraces.

Joel recommends even going so far as automatically creating tickets in your bug tracker when your application experiences a failure. Personally, I create a (private) RSS feed with the stacktraces, urls, etc. that the developers can subscribe to.

Showing stack traces to your users on the other hand could possibly leak information that malicious users could use to attack your site. Overly detailed error messages are one of the classic stepping stones to SQL injection attacks.

Edit (added code sample to capture traceback):

You can get the exception information from the sys.exc_info call. While formatting the traceback for display comes from the traceback module:

import traceback
import sys

	raise Exception("Message")
	type, value, tb = sys.exc_info()
	print >> sys.stderr,  type.__name__, ":", value
	print >> sys.stderr, '\n'.join(traceback.format_tb(tb))


Exception : Message
  File "exception.py", line 5, in <module>
    raise Exception("Message")
share|improve this answer
Ah...good point. Can you shed some light on the best approach to log each time a 500 error occurs (and include the stacktrace)? –  Huuuze Sep 23 '08 at 14:46
Edited to add how I handle logging stack trances: private RSS feed for the developers. –  Aaron Maenpaa Sep 23 '08 at 14:49
Code sample would be nice... –  S.Lott Sep 23 '08 at 16:07
Most of the time when we're debugging, we need a traceback and reproducible steps. We have the traceback, but the customer has the reproducible steps. Linking them via some kind of message to the user is helpful. Is there no way we can provide some kind of message to the user? –  isaaclw Jul 13 '12 at 18:26
Note: my plan is some kind of hash, providing a trace is a bad idea. –  isaaclw Jul 13 '12 at 18:27
add comment

As @zacherates says, you really don't want to display a stacktrace to your users. The easiest approach to this problem is what Django does by default if you have yourself and your developers listed in the ADMINS setting with email addresses; it sends an email to everyone in that list with the full stack trace (and more) everytime there is a 500 error with DEBUG = False.

share|improve this answer
add comment

You could call sys.exc_info() in a custom exception handler. But I don't recommend that. Django can send you emails for exceptions.

share|improve this answer
add comment

If we want to show exceptions which are generated , on ur template(500.html) then we could write your own 500 view, grabbing the exception and passing it to your 500 template.


#.In views.py:

import sys,traceback

def custom_500(request):
t = loader.get_template('500.html')

print sys.exc_info()
type, value, tb = sys.exc_info()
return HttpResponseServerError(t.render(Context({
'exception_value': value,
'tb':traceback.format_exception(type, value,


#.In Main Urls.py:

from django.conf.urls.defaults import *
handler500 = 'project.web.services.views.custom_500'

#.In Template(500.html):

{{ exception_value }}{{value}}{{tb}}

more about it here: https://docs.djangoproject.com/en/dev/topics/http/views/#the-500-server-error-view

share|improve this answer
add comment

Your Answer


