20

Django's runserver command doesn't output a stack trace when I append --traceback --verbosity 2:

➫ python manage.py runserver --traceback --verbosity 2
Validating models...

0 errors found
July 24, 2013 - 11:45:12
Django version 1.5.1, using settings 'base.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[24/Jul/2013 11:45:27] "POST /login/get_associations/ HTTP/1.0" 500 13220

Are there other command line switches or logging configuration I can add to get runserver to print a stack trace when there is a 500?

Mark L
  • 12,405
  • 4
  • 28
  • 41
  • I don't think there is any such switch. What we usually do is use `DEBUG=True`, in which case the traceback and other information goes to the browser. Why is this not sufficient? – Antonis Christofides Jul 24 '13 at 12:05
  • DEBUG is set to True already. I'm trying to debug an AJAX-based system. – Mark L Jul 24 '13 at 12:08
  • Then probably you want this: http://stackoverflow.com/questions/14171520/how-to-debug-the-ajax-request-in-django. I think you can also use Firebug, which is not mentioned in that thread. – Antonis Christofides Jul 24 '13 at 12:11
  • I'm doing something like that at the moment where I'm watching the response from the Chrome WebKit Debugger and reading through the HTML stack trace. Ideally I'd like to just be able to read the stack trace in the command line or tail a log file. – Mark L Jul 24 '13 at 12:22
  • http://djangosnippets.org/snippets/420/ ; https://docs.djangoproject.com/en/dev/topics/http/middleware/#process_exception – dm03514 Jul 24 '13 at 13:13
  • You can install firebug plugin in firefox browser, and can see any kind of errors in the firebug panel. – sandeep Jul 24 '13 at 13:22
  • possible duplicate of [Print a stack trace to stdout on errors in Django while using manage.py runserver](http://stackoverflow.com/questions/5886275/print-a-stack-trace-to-stdout-on-errors-in-django-while-using-manage-py-runserve) – Victor Sergienko Sep 02 '15 at 08:44

1 Answers1

31

Agreed that this is convenient, especially for MVVM-centric app development (e.g. Angular/Ember front-end). Also this is helpful when others are testing out the front-end.

As you mentioned, this isn't provided by DEBUG=True. You can add a stacktrace when running ./manage.py runserver by adding the following to the settings.py file:

LOGGING = {
    'version': 1,
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['console'],
            'propagate': True,
            'level': 'DEBUG',
        },
    },
}

This syntax comes from the Django documentation Configuring Logging and can be further modified to increase or decrease the amount of console-logging.

Also note that 5XX responses are raised as ERROR messages and 4XX responses are raised as WARNING messages.

likeon
  • 791
  • 1
  • 6
  • 19
Mike Biglan MS
  • 1,822
  • 1
  • 20
  • 20