且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何找到我在Django中遇到的500错误?

更新时间:2023-02-19 17:16:17

基本的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'