Living on the Edge

Werkzeug Debugger in Django

Posted on June 21, 2008

A new feature to come to the Django-Command-Extensions project is the implementation of a new management command called runserver_plus. For now the “plus” part means that I’ve replaced the standard Django traceback pages with the Werkzeug Debugger traceback page in it’s place.

Getting Started

This item requires that you have the Werkzeug WSGI utilities installed, in addition to the django-command-extensions app.

To get started we just use the `runserver_plus` command instead of the normal `runserver` command:


$ python manage.py runserver_plus

 * Running on http://127.0.0.1:8000/
 * Restarting with reloader...
Validating models...
0 errors found

Django version 0.97-newforms-admin-SVN-unknown, using settings 'screencasts.settings'
Development server is running at http://127.0.0.1:8000/
Using the Werkzeug debugger (http://werkzeug.pocoo.org/)
Quit the server with CONTROL-C.

Note: all normal runserver options apply. In other words, if you need to change the port number or the host information, you can do so like you would normally.

Using

Whenever we hit an exception in our code, instead of the normal Django traceback page appearing, we see the Werkzeug traceback page instead.

Along with the typical traceback information we have a couple of options. These options appear when you hover over a particular traceback line. Notice that two buttons appear to the right:

The options are:

View Source

This displays the source below the traceback:

Being able to view the source file is handy because you are able to get more context information around where the error occurred. The actual traceback areas are highlighted so they are easy to spot.

One awkward piece about this is that the page is not scrolled to the bottom. At first I thought nothing was happening because of this.

Interactive Debugging Console

When you click on this button a new pane will open up below the traceback line you’re on. This is the money shot:

An ajax based console appears in the pane and you can begin debugging away. Notice in the screenshot above I did a print environ to see what was in the environment parameter coming into the function.

Summary

I’ve only been using the Werkzeug debugger on my Django projects for a day now. But I’m loving it. I encourage you to check it out. Plus if you’re not a current Django-Command-Extensions user, you’re going to find tons of really cool additions in there.

WARNING: This should never be used in any kind of production environment. Not even for a quick check into a problem. I cannot emphasize this enough. The interactive debugger allows you to evaluate python code right against the server. You’ve been warned.

Comments
  1. AlexJune 21, 2008 @ 11:15 PM

    I wonder how difficult it would be to add the ajax console to django’s error pages, that seems to be the one real advantage.

  2. JeffJune 22, 2008 @ 12:57 AM

    Agreed; Django’s existing error pages are chock-full of well-laid-out information. All I can see that this Werkzeug debugger adds is the Ajax debugger. Not that it’s a trivial addition—it looks awesome!

    But I personally think we’d be better off merging that functionality into Django core, if possible, instead of encouraging reinvention of the wheel w/r/t the rest of the traceback/source code display.

  3. Armin RonacherJune 22, 2008 @ 07:00 AM

    Long before I wrote the Werkzeug debugger I implemented a AJAX console to the Django error pages. The patches for that are still lurking around in the Django trac somewhere.

    But they were never commited for security reasons.

    Regards, Armin

  4. EmptyJune 22, 2008 @ 11:02 AM

    Armin: Nice work and thanks for visiting. UPDATE: Armin let me in on a very cool tip, use dump(environ) instead and you’ll get a very nice output.

  5. Andy BakerJune 22, 2008 @ 07:47 PM

    This is fab and I will try it out. However Werkzeug is a rather large install. It would be good if we could extract just the parts needed to run this.

  6. Karen TraceyJune 22, 2008 @ 08:28 PM

    This is the ticket (with patches) where the Ajax debug console was proposed for Django:

    http://code.djangoproject.com/ticket/3527

    Sounds like if it could be made pluggable somehow, then it might have a chance of getting in.

  7. NickJune 23, 2008 @ 09:14 AM

    Thank you very much Empty, Armin and all people who works on this extension! django-command-extensions grows very fast and is the most useful extension for everyday developer job. And now you have that astonishing Werkzeug debugger!

  8. trbsJune 23, 2008 @ 01:38 PM

    Very cool :)

    Between runserver_plus, support for indexes in sqldiff and various smaller fixes, this warrants a bump in the version number and a release :)

    Version 0.3 is build and uploaded in code.google.com (Ofcourse if you use svn or mercurial you can just ‘svn up’ your repository :)

  9. NickJune 23, 2008 @ 06:13 PM

    Please make a command something like: recreate_db to erase db, create new and create superuser with default login/pass automatically. This will save a lot of time.

    Thanks!

  10. HumphreyJune 25, 2008 @ 12:36 AM

    Very cool debugger. Very easy to install, and use. I reckon this could become one of the next big “This is why django is better than x” things.