我如何找到我在Django中遇到的500错误? [英] How can I find out what 500 error am I getting in Django?

查看:200
本文介绍了我如何找到我在Django中遇到的500错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我收到这个追溯。

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模板 - 而不是实际的服务器错误。我如何找出服务器错误是什么?

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?

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

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.

推荐答案

基本的500.html是如Alasdair所说,方便有用,可能会揭示一些敏感的东西。

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

但是,如果以负责任的方式调试网络是目标,那么您的网站模板目录的基本nondefault500.html模板将看起来像

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

,需要进行一个URLconf调整,

and there would need to be a URLconf adjustment made,

handler500 = 'mysite.views.this_server_error'

这篇关于我如何找到我在Django中遇到的500错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆