dangerChihuahua007
dangerChihuahua007

Reputation: 20895

How can I find out what 500 error am I getting in Django?

When I visit a page (http://68.123.151.234/static/quickstart.html) in my Django application served on a server spawned by Django, the page reads

A server error occurred.  Please contact the administrator.

And I receive this traceback.

Traceback (most recent call last):
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 241, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 153, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 228, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/defaults.py", line 32, in server_error
    t = loader.get_template(template_name) # You need to create a 500.html template.
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 145, in get_template
    template, origin = find_template(template_name)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 138, in find_template
    raise TemplateDoesNotExist(name)
TemplateDoesNotExist: 500.html
[17/May/2012 11:31:45] "GET /static/quickstart.html HTTP/1.1" 500 59
Traceback (most recent call last):
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 241, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 153, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 228, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/defaults.py", line 32, in server_error
    t = loader.get_template(template_name) # You need to create a 500.html template.
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 145, in get_template
    template, origin = find_template(template_name)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 138, in find_template
    raise TemplateDoesNotExist(name)
TemplateDoesNotExist: 500.html

This traceback only tells me that I need a 500 template - not what the actual server error is. How can I find out what the server error was?

I checked out Django documentation (https://docs.djangoproject.com/en/1.4/topics/http/views/), which directs me to create a 500 error template as a potential solution. However, it gives no instructions as to how to display the error within such a template.

Upvotes: 6

Views: 12916

Answers (4)

rhoyerboat
rhoyerboat

Reputation: 204

A basic 500.html which is convenient and useful would, as Alasdair said, potentially reveal something sensitive.

However, if debugging over the web in a responsible way is the objective, a basic nondefault500.html template for your site template dir would look like

<html><head><body>
<!-- starting with sys.exc_info but hey, it's python -->
Type: {{ type }} <br />
Value: {{ value }} <br />
Traceback: {{ traceback }} <br />
</body></head></html>

and the new handler would populate that context as such:

def this_server_error(request, template_name='nondefault500.html'):
    """
    500 error handler.

    Templates: `500.html`
    Context: sys.exc_info() results
     """
    t = loader.get_template(template_name) # You need to create a 500.html template.
    ltype,lvalue,ltraceback = sys.exc_info()
    sys.exc_clear() #for fun, and to point out I only -think- this hasn't happened at 
                    #this point in the process already
    return http.HttpResponseServerError(t.render(Context({'type':ltype,'value':lvalue,'traceback':ltraceback})))

and there would need to be a URLconf adjustment made,

handler500 = 'mysite.views.this_server_error'

Upvotes: 7

user1151618
user1151618

Reputation:

TemplateDoesNotExist: 500.html

I think you should create 500.html in your template dir.

Upvotes: 1

Alasdair
Alasdair

Reputation: 308789

If you are testing your site, set DEBUG=True then Django will show the traceback.

Once the site is live, you probably don't want to display the traceback on the error page, as it may contain sensitive information.

If you add a 500 template. Django will then send an email containing the traceback to the users listed in the ADMINS setting.

See the docs on Error Reporting for more info.

Upvotes: 5

dm03514
dm03514

Reputation: 55952

I believe you need a 500.html template when DEBUG is set to False.

Create one and put it in your template directory

It will be shown to a user whenever there is a 500 error.

Upvotes: 2

Related Questions