2012-05-17 37 views
6

Khi tôi ghé thăm một trang (http://68.123.151.234/static/quickstart.html) trong ứng dụng Django tôi phục vụ trên một máy chủ sinh ra bởi Django, trang đọcLàm cách nào tôi có thể tìm ra 500 lỗi nào tôi nhận được ở Django?

A server error occurred. Please contact the administrator. 

Và tôi nhận traceback này.

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 

Lần truy nguyên này chỉ cho tôi biết rằng tôi cần 500 mẫu - không phải lỗi máy chủ thực sự là gì. Làm thế nào tôi có thể tìm ra lỗi máy chủ là gì?

Tôi đã xem tài liệu Django (https://docs.djangoproject.com/en/1.4/topics/http/views/), hướng dẫn tôi tạo mẫu lỗi 500 làm giải pháp tiềm năng. Tuy nhiên, nó không đưa ra hướng dẫn nào về cách hiển thị lỗi trong khuôn mẫu như vậy.

+1

'DEBUG' của bạn có được đặt thành' True' không? Django sẽ không hiển thị các dấu vết với 'DEBUG' được đặt thành' False'. – dgel

Trả lời

7

Một 500.html cơ bản thuận tiện và hữu ích, như Alasdair đã nói, có khả năng tiết lộ điều gì đó nhạy cảm.

Tuy nhiên, nếu gỡ lỗi trên web một cách có trách nhiệm là mục tiêu, một nondefault500.html mẫu cơ bản cho trang web của bạn mẫu dir sẽ trông như thế

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

và xử lý mới sẽ cư mà bối cảnh như vậy :

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

và sẽ cần phải là một điều chỉnh URLconf thực hiện,

handler500 = 'mysite.views.this_server_error' 
+0

Cảm ơn! Điều đó làm các trick. – dangerChihuahua007

2

Tôi tin rằng bạn cần một mẫu 500.html khi DEBUG được đặt thành False.

Tạo một tài khoản và đặt nó trong thư mục mẫu của bạn

Nó sẽ được hiển thị cho người dùng bất cứ khi nào có lỗi 500.

5

Nếu bạn đang thử nghiệm trang web của mình, hãy đặt DEBUG=True thì Django sẽ hiển thị truy xuất lại.

Khi trang web hoạt động, có thể bạn không muốn hiển thị truy nguyên trên trang lỗi vì nó có thể chứa thông tin nhạy cảm.

Nếu bạn thêm 500 mẫu. Django sau đó sẽ gửi một email chứa traceback cho người dùng được liệt kê trong cài đặt ADMINS.

Xem tài liệu trên Error Reporting để biết thêm thông tin.

1
TemplateDoesNotExist: 500.html 

Tôi nghĩ bạn nên tạo 500.html trong thư mục mẫu của mình.

Các vấn đề liên quan