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

前端 未结 5 954
孤街浪徒
孤街浪徒 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条回答
  • 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

    0 讨论(0)
  • 2020-12-29 05:45

    I know this is an old question, but these days I would recommend using a service such as Sentry to capture your errors.

    On Django, the steps to set this up are incredibly simple. From the docs:

    • Install Raven using pip install raven
    • Add 'raven.contrib.django.raven_compat' to your settings.INSTALLED_APPS.
    • Add RAVEN_CONFIG = {"dsn": YOUR_SENTRY_DSN} to your settings.

    Then, on your 500 page (defined in handler500), pass the request.sentry.id to the template and your users can reference the specific error without any of your internals being exposed.

    0 讨论(0)
  • 2020-12-29 05:57

    As @zacherates says, you really don't want to display a stacktrace to your users. The easiest approach to this problem is what Django does by default if you have yourself and your developers listed in the ADMINS setting with email addresses; it sends an email to everyone in that list with the full stack trace (and more) everytime there is a 500 error with DEBUG = False.

    0 讨论(0)
  • 2020-12-29 06:03

    Automatically log your 500s, that way:

    • You know when they occur.
    • You don't need to rely on users sending you stacktraces.

    Joel recommends even going so far as automatically creating tickets in your bug tracker when your application experiences a failure. Personally, I create a (private) RSS feed with the stacktraces, urls, etc. that the developers can subscribe to.

    Showing stack traces to your users on the other hand could possibly leak information that malicious users could use to attack your site. Overly detailed error messages are one of the classic stepping stones to SQL injection attacks.

    Edit (added code sample to capture traceback):

    You can get the exception information from the sys.exc_info call. While formatting the traceback for display comes from the traceback module:

    import traceback
    import sys
    
    try:
        raise Exception("Message")
    except:
        type, value, tb = sys.exc_info()
        print >> sys.stderr,  type.__name__, ":", value
        print >> sys.stderr, '\n'.join(traceback.format_tb(tb))
    

    Prints:

    Exception : Message
      File "exception.py", line 5, in <module>
        raise Exception("Message")
    
    0 讨论(0)
  • 2020-12-29 06:10

    You could call sys.exc_info() in a custom exception handler. But I don't recommend that. Django can send you emails for exceptions.

    0 讨论(0)
提交回复
热议问题