Giter VIP home page Giter VIP logo

django-pdb's Introduction

Status: I no longer actively use this project, and am only accepting bugfixes and django compatibility pull requests.

Django PDB

Make debugging Django easier

Adding pdb.set_trace() to your source files every time you want to break into pdb sucks.

Don't do that.

Do this.

Installation

Install using pip:

pip install django-pdb

Add it to your settings.py.

For Django before 1.7 it needs to be added AFTER any apps that override the runserver or test commands (includes south and django.contrib.staticfiles).

For Django after 1.7 it needs to be added BEFORE them.

# Order is important and depends on your Django version.
# With Django 1.7+ put it towards the beginning, otherwise towards the end.
INSTALLED_APPS = (
    ...
    'django_pdb',
    ...
)

# Make sure to add PdbMiddleware after all other middleware.
# PdbMiddleware only activates when settings.DEBUG is True.
MIDDLEWARE_CLASSES = (
    ...
    'django_pdb.middleware.PdbMiddleware',
)

Usage

manage.py runserver

Drops into pdb at the start of a view if the URL includes a pdb GET parameter.

Drops into ipdb at the start of a view if the URL includes a ipdb GET parameter.

This behavior is only enabled if settings.DEBUG = True:

bash: testproject/manage.py runserver
Validating models...

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

GET /test?pdb
function "myview" in testapp/views.py:7
args: ()
kwargs: {}

> /Users/tom/github/django-pdb/testproject/testapp/views.py(8)myview()
-> a = 1
(Pdb)

manage.py runserver --pdb or manage.py runserver --ipdb

Drops into pdb/ipdb at the start of every view:

bash: testproject/manage.py runserver --pdb
Validating models...

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

GET /test
function "myview" in testapp/views.py:7
args: ()
kwargs: {}

> /Users/tom/github/django-pdb/testproject/testapp/views.py(7)myview()
-> a = 1
(Pdb)

manage.py test --pdb or manage.py test --ipdb

Drops into pdb/ipdb on test errors/failures:

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

> /Users/tom/github/django-pdb/testproject/testapp/tests.py(16)test_error()
-> one_plus_one = four
(Pdb)

Post mortem mode

manage.py runserver --pm

Post mortem mode, drops into (i)pdb if an exception is raised in a view. This works only if there is no other app overriding runserver command.

POST_MORTEM = True

You can also add `POST_MORTEM = True` to your `settings.py` to enable this option even if other app overrides `runserver`.

Filter

You can also use the template filter pdb or ipdb to explore a template variable in (i)pdb this way:

{% load pdb %}

{{ variable|pdb }}
{{ variable|ipdb }}
{{ variable|ipdb|a_filter_to_debug }}

Example:

bash: testproject/manage.py runserver
Validating models...

0 errors found
Django version 1.4, using settings 'testproject.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
> /Users/tom/github/django-pdb/django_pdb/templatetags/pdb_filters.py(14)pdb()
-> return element
(Pdb) element
"I'm the variable"
(Pdb) element = "another value"
(Pdb) c
[11/May/2012 11:22:53] "GET /filter/ HTTP/1.1" 200 37

This is useful to inspect a complex object that isn't behaving as expected or debug a filter.

Other apps that override test/runserver

manage.py test --pdb works if you also have other apps that override the test command, as long as they use Python's unittest framework.

Make sure to put django_pdb after any conflicting apps in INSTALLED_APPS so that they have priority.

django-pdb's People

Contributors

blueyed avatar demianbrecht avatar goinnn avatar johnjohndoe avatar jpulec avatar pielgrzym avatar psycojoker avatar schinckel avatar sfllaw avatar shearichard avatar tomchristie avatar yhlam avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.