如何找出Django中出现的500错误?

时间:2012-05-17 16:34:58

标签: django debugging

当我在由Django产生的服务器上提供的Django应用程序中访问页面(http://68.123.151.234/static/quickstart.html)时,该页面显示

A server error occurred.  Please contact the administrator.

我收到了这个追溯。

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

这个回溯只告诉我需要一个500模板 - 而不是实际的服务器错误。如何找出服务器错误是什么?

我检查了Django文档(https://docs.djangoproject.com/en/1.4/topics/http/views/),它指示我创建一个500错误模板作为潜在的解决方案。但是,它没有说明如何在这样的模板中显示错误。

4 个答案:

答案 0 :(得分:7)

正如阿拉斯戴尔所说,一个方便实用的基本500.html可能会揭示敏感的东西。

但是,如果以负责任的方式通过网络进行调试是目标,则网站模板目录的基本nondefault500.html模板将如下所示

<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>

并且新的处理程序将填充该上下文:

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})))

并且需要进行URLconf调整,

handler500 = 'mysite.views.this_server_error'

答案 1 :(得分:5)

如果您正在测试您的网站,请设置DEBUG=True,然后Django将显示回溯。

网站上线后,您可能不希望在错误页面上显示回溯,因为它可能包含敏感信息。

如果添加500模板。然后,Django将向ADMINS设置中列出的用户发送包含回溯的电子邮件。

有关详细信息,请参阅Error Reporting上的文档。

答案 2 :(得分:2)

我认为当DEBUG设置为False时,您需要500.html模板。

创建一个并将其放入模板目录

只要出现500错误,就会向用户显示。

答案 3 :(得分:1)

TemplateDoesNotExist: 500.html

我认为您应该在模板目录中创建500.html。