How do I include a stacktrace in my Django 500.html page?

前端 未结 5 955
孤街浪徒
孤街浪徒 2020-12-29 05:28

I\'m running Django 1.0 and I\'m close to deploying my app. As such, I\'ll be changing the DEBUG setting to False.

With that being said, I\'d still lik

5条回答
  •  -上瘾入骨i
    2020-12-29 05:43

    If we want to show exceptions which are generated , on ur template(500.html) then we could write your own 500 view, grabbing the exception and passing it to your 500 template.

    Steps:

    # In views.py:

    import sys,traceback
    
    def custom_500(request):
        t = loader.get_template('500.html')
    
        print sys.exc_info()
        type, value, tb = sys.exc_info()
        return HttpResponseServerError(t.render(Context({
            'exception_value': value,
            'value':type,
            'tb':traceback.format_exception(type, value, tb)
        },RequestContext(request))))
    

    # In Main urls.py:

    from django.conf.urls.defaults import *
    handler500 = 'project.web.services.views.custom_500'
    

    # In Template(500.html):

    {{ exception_value }}{{value}}{{tb}}
    

    more about it here: https://docs.djangoproject.com/en/dev/topics/http/views/#the-500-server-error-view

提交回复
热议问题