How to debug in Django, the good way?

0 votes
asked Jul 13, 2009 by googletorp

So, I started learning to code in Python and later Django. The first times it was hard looking at tracebacks and actually figure out what I did wrong and where the syntax error was. Some time has passed now and some way along the way, I guess I got a routine in debugging my Django code. As this was done early in my coding experience, I sat down and wondered if how I was doing this was ineffective and could be done faster. I usually manage to find and correct the bugs in my code, but I wonder if I should be doing it faster?

I usually just use the debug info Django gives when enabled. When things do end up as I thought it would, I break the code flow a lot with a syntax error, and look at the variables at that point in the flow to figure out, where the code does something other than what I wanted.

But can this be improved? Are there some good tools or better ways to debug your Django code?

26 Answers

0 votes
answered Jan 13, 2009 by gath

I use pyDev with Eclipse really good, set break points, step into code, view values on any objects and variables, try it.

0 votes
answered Jul 13, 2009 by simplyharsh

There are a bunch of ways to do it, but the most straightforward is to simply use the Python debugger. Just add following line in to a Django view function:

import pdb; pdb.set_trace()

If you try to load that page in your browser, the browser will hang and you get a prompt to carry on debugging on actual executing code.

However there are other options suggested by others (I won't recommend them):

* return HttpResponse({variable to inspect})

* print {variable to inspect}

* raise Exception({variable to inspect})

But the Python Debugger (pdb) is highly recommended for all types of Python code. If you are already into pdb, you'd also want to have a look at IPDB that uses ipython for debugging. Happy Coding.

A useful reference provided by Seafangs : Using the Python debugger in Django

0 votes
answered Jul 13, 2009 by van-gale

There are a few tools that cooperate well and can make your debugging task easier.

Most important is the Django debug toolbar.

Then you need good logging using the Python logging facility. You can send logging output to a log file, but an easier option is sending log output to firepython. To use this you need to use the Firefox browser with the firebug extension. Firepython includes a firebug plugin that will display any server-side logging in a Firebug tab.

Firebug itself is also critical for debugging the Javascript side of any app you develop. (Assuming you have some JS code of course).

I also liked django-viewtools for debugging views interactively using pdb, but I don't use it that much.

There are more useful tools like dozer for tracking down memory leaks (there are also other good suggestions given in answers here on SO for memory tracking).

0 votes
answered Jul 13, 2009 by benjamin-wohlwend

I really like Werkzeug's interactive debugger. It's similar to Django's debug page, except that you get an interactive shell on every level of the traceback. If you use the django-extensions, you get a runserver_plus managment command which starts the development server and gives you Werkzeug's debugger on exceptions.

Of course, you should only run this locally, as it gives anyone with a browser the rights to execute arbitrary python code in the context of the server.

0 votes
answered Jul 12, 2010 by phoebeb

I use PyCharm (same pydev engine as eclipse). Really helps me to visually be able to step through my code and see what is happening.

0 votes
answered Jul 24, 2010 by koobz

A little quickie for template tags:

def pdb(element):
    import pdb; pdb.set_trace()
    return element

Now, inside a template you can do {{ template_var|pdb }} and enter a pdb session (given you're running the local devel server) where you can inspect element to your heart's content.

It's a very nice way to see what's happened to your object when it arrives at the template.

0 votes
answered Jul 24, 2010 by tomasz-zielinski

Almost everything has been mentioned so far, so I'll only add that instead of pdb.set_trace() one can use ipdb.set_trace() which uses iPython and therefore is more powerful (autocomplete and other goodies). This requires ipdb package, so you only need to pip install ipdb

0 votes
answered Jan 30, 2011 by tom-christie

I've pushed django-pdb to PyPI. It's a simple app that means you don't need to edit your source code every time you want to break into pdb.

Installation is just...

  1. pip install django-pdb
  2. Add 'django_pdb' to your INSTALLED_APPS

You can now run: runserver --pdb to break into pdb at the start of every view...

bash: runserver --pdb
Validating models...

0 errors found
Django version 1.3, using settings 'testproject.settings'
Development server is running at
Quit the server with CONTROL-C.

function "myview" in testapp/
args: ()
kwargs: {}

> /Users/tom/github/django-pdb/testproject/testapp/
-> a = 1

And run: test --pdb to break into pdb on test failures/errors...

bash: test testapp --pdb
Creating test database for alias 'default'...
>>> test_error (testapp.tests.SimpleTest)
Traceback (most recent call last):
  File ".../django-pdb/testproject/testapp/", line 16, in test_error
    one_plus_one = four
NameError: global name 'four' is not defined

> /Users/tom/github/django-pdb/testproject/testapp/
-> one_plus_one = four

The project's hosted on GitHub, contributions are welcome of course.

0 votes
answered Jan 6, 2012 by user1144616

If using Aptana for django development, watch this:

If not, consider using it.

0 votes
answered Jan 6, 2012 by 029xue

The easiest way to debug python - especially for programmers that are used to Visual Studio - is using PTVS (Python Tools for Visual Studio). The steps are simple:

  1. Download and install it from
  2. Set breakpoints and press F5.
  3. Your breakpoint is hit, you can view/change the variables as easy as debugging C#/C++ programs.
  4. That's all :)

If you want to debug Django using PTVS, you need to do the following:

  1. In Project settings - General tab, set "Startup File" to "", the entry point of the Django program.
  2. In Project settings - Debug tab, set "Script Arguments" to "runserver --noreload". The key point is the "--noreload" here. If you don't set it, your breakpoints won't be hit.
  3. Enjoy it.
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter