2012-05-17 50 views
6

當我在我的Django應用程序訪問的網頁(http://68.123.151.234/static/quickstart.html)通過Django的催生了一個服務器上提供服務,頁面讀取我如何才能知道我在Django中遇到了哪些500錯誤?

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錯誤模板作爲潛在的解決方案。但是,它沒有提供關於如何在這樣的模板中顯示錯誤的指示。

+1

你的'DEBUG'是否設置爲'True'? Django不會以'DEBUG'設置爲'False'顯示回溯。 – dgel

回答

7

正如Alasdair所說,一個基本的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}))) 

,有需要是由URL配置調整,

handler500 = 'mysite.views.this_server_error' 
+0

謝謝!這就是訣竅。 – dangerChihuahua007

2

我相信你需要一個500.html模板,當DEBUG設置爲False

創建一個並把它放在你的模板目錄

將顯示給用戶,只要有一個500錯誤。

5

如果您正在測試您的網站,請設置DEBUG=True然後Django將顯示回溯。

一旦網站正常運行,您可能不希望在錯誤頁面上顯示回溯,因爲它可能包含敏感信息。

如果添加500個模板。然後,Django將向ADMINS設置中列出的用戶發送包含回溯的電子郵件。

有關更多信息,請參閱Error Reporting上的文檔。

1
TemplateDoesNotExist: 500.html 

我想你應該在你的模板目錄中創建500.html。

相關問題