Django


Debugging All Versions

1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
1.10

Improvements requested by Ashish Gupta:

  • This topic would benefit from examples that don't currently exist. - Sep 21 at 9:18
    Comments:
    • Adding django-extensions example will help. It makes debugging easier with commands like shell_plus, runserver_plus, etc. - Ashish Gupta

This draft deletes the entire topic.

inline side-by-side expand all collapse all

Examples

  • 2

    First, you need to install django-debug-toolbar:

    pip install django-debug-toolbar
    

    Next, include it to project's installed apps, but be careful - it's always a good practice to use a different settings.py file for such development-only apps and middlewares as debug toolbar:

    # If environment is dev...
    DEBUG = True
    
    INSTALLED_APPS += [
        'debug_toolbar',
    ]
    

    Debug toolbar also relies on static files, so appropriate app should be included as well:

    INSTALLED_APPS = [
        # ...
        'django.contrib.staticfiles',
        # ...
    ]
    
    STATIC_URL = '/static/'
    
    # If environment is dev...
    DEBUG = True
    
    INSTALLED_APPS += [
        'debug_toolbar',
    ]
    

    Collect toolbar's static after installation:

    python manage.py collectstatic
    

    That's it, debug toolbar will appear on you project's pages, providing various useful information about execution time, SQL, static files, signals, etc.

    Also, django-debug-toolbar requires a Content-type of text/html, <html> and <body> tags to render properly.

  • 2

    Most basic Django debugging tool is pdb, a part of Python standard library.

    Init view script

    Let's examine a simple views.py script:

    from django.http import HttpResponse
    
    
    def index(request):
        foo = 1
        bar = 0
    
        bug = foo/bar
    
        return HttpResponse("%d goes here." % bug)
    

    Console command to run server:

    python manage.py runserver
    

    It's obvious that Django would throw a ZeroDivisionError when you try to load index page, but if we'll pretend that the bug is very deep in the code, it could get really nasty.

    Setting a breakpoint

    Fortunately, we can set a breakpoint to trace down that bug:

    from django.http import HttpResponse
    
    # Pdb import
    import pdb;
    
    
    def index(request):
        foo = 1
        bar = 0
        
        # This is our new breakpoint
        pdb.set_trace()
        
        bug = foo/bar
        
        return HttpResponse("%d goes here." % bug)
    

    Console command to run server with pdb:

    python -m pdb manage.py runserver
    

    Now on page load breakpoint will trigger (Pdb) prompt in the shell, which will also hang your browser in pending state.

    Debugging with pdb shell

    It's time to debug that view by interacting with script via shell:

    > ../views.py(12)index()
    -> bug = foo/bar
    # input 'foo/bar' expression to see division results:
    (Pdb) foo/bar
    *** ZeroDivisionError: division by zero
    # input variables names to check their values:
    (Pdb) foo
    1
    (Pdb) bar
    0
    # 'bar' is a source of the problem, so if we set it's value > 0...
    (Pdb) bar = 1
    (Pdb) foo/bar
    1.0
    # exception gone, ask pdb to continue execution by typing 'c':
    (Pdb) c
    [03/Aug/2016 10:50:45] "GET / HTTP/1.1" 200 111
    

    In the last line we see that our view returned an OK response and executing as it should.

    To stop pdb loop, just input q in a shell.

I am downvoting this example because it is...

Syntax

Syntax

Parameters

Parameters

Remarks

Pdb

Pdb can also print out all existing variables in global or local scope, by typing globals() or locals() in (Pdb) prompt respectively.

Still have a question about Debugging? Ask Question

Using Django Debug Toolbar

2

First, you need to install django-debug-toolbar:

pip install django-debug-toolbar

Next, include it to project's installed apps, but be careful - it's always a good practice to use a different settings.py file for such development-only apps and middlewares as debug toolbar:

# If environment is dev...
DEBUG = True

INSTALLED_APPS += [
    'debug_toolbar',
]

Debug toolbar also relies on static files, so appropriate app should be included as well:

INSTALLED_APPS = [
    # ...
    'django.contrib.staticfiles',
    # ...
]

STATIC_URL = '/static/'

# If environment is dev...
DEBUG = True

INSTALLED_APPS += [
    'debug_toolbar',
]

Collect toolbar's static after installation:

python manage.py collectstatic

That's it, debug toolbar will appear on you project's pages, providing various useful information about execution time, SQL, static files, signals, etc.

Also, django-debug-toolbar requires a Content-type of text/html, <html> and <body> tags to render properly.

Using Python Debugger (Pdb)

2

Most basic Django debugging tool is pdb, a part of Python standard library.

Init view script

Let's examine a simple views.py script:

from django.http import HttpResponse


def index(request):
    foo = 1
    bar = 0

    bug = foo/bar

    return HttpResponse("%d goes here." % bug)

Console command to run server:

python manage.py runserver

It's obvious that Django would throw a ZeroDivisionError when you try to load index page, but if we'll pretend that the bug is very deep in the code, it could get really nasty.

Setting a breakpoint

Fortunately, we can set a breakpoint to trace down that bug:

from django.http import HttpResponse

# Pdb import
import pdb;


def index(request):
    foo = 1
    bar = 0
    
    # This is our new breakpoint
    pdb.set_trace()
    
    bug = foo/bar
    
    return HttpResponse("%d goes here." % bug)

Console command to run server with pdb:

python -m pdb manage.py runserver

Now on page load breakpoint will trigger (Pdb) prompt in the shell, which will also hang your browser in pending state.

Debugging with pdb shell

It's time to debug that view by interacting with script via shell:

> ../views.py(12)index()
-> bug = foo/bar
# input 'foo/bar' expression to see division results:
(Pdb) foo/bar
*** ZeroDivisionError: division by zero
# input variables names to check their values:
(Pdb) foo
1
(Pdb) bar
0
# 'bar' is a source of the problem, so if we set it's value > 0...
(Pdb) bar = 1
(Pdb) foo/bar
1.0
# exception gone, ask pdb to continue execution by typing 'c':
(Pdb) c
[03/Aug/2016 10:50:45] "GET / HTTP/1.1" 200 111

In the last line we see that our view returned an OK response and executing as it should.

To stop pdb loop, just input q in a shell.

Topic Outline