Debugging Django



Simon Willison
DJUGL, 19th May 2008
This talk is not about

 Test Driven
Development

               http://www.flickr.com/photos/alikaragoz/209296304/
This talk is about

 Bug Driven
Development
http://www.shipmentoffail.com/fails/2008/04/horse-vs-car-fail/
Make the most of the error page

Print statements and logging

Using the debugger

Catching errors in production

Abusing ...
The Django error page

  It’s not just for errors!
  Trigger it explicitly with “assert False”
  Show a value with “assert...
Logging to your console
def index(req):
    print quot;Hello there!quot;


[19/May/2008 18:14:39] quot;GET /static/css/img...
Logging to your console
# in settings.py
import logging
logging.basicConfig(
    level = logging.DEBUG,
    format = '%(as...
Logging to a file
# in settings.py
import logging
logging.basicConfig(
    level = logging.DEBUG,
    format = '%(asctime)s...
Logging the calling context

import logging, traceback, pprint

def my_buggy_function(arg):
    context = pprint.pformat(t...
Using the debugger

import pdb; pdb.set_trace()

$ python -i ./manage.py ...
...
>>> import pdb; pdb.pm()
Errors in production
Two misleadingly-named settings:
# Receive 500 error e-mails if not DEBUG
ADMINS = (
    ('Simon Will...
Errors over XMPP
db-error-log
Custom error middleware
class DBLogMiddleware(object):
    def process_exception(self, request, exception):
        server...
More useful middleware

  ProfilerMiddleware
   See profiler output with url?prof
  DebugFooter
   SQL and templates logged ...
Abusing the test client

from django.test.utils import setup_test_environment
setup_test_environment()

from django.test.c...
Test Driven Development


  www.djangoproject.com/documentation/testing/
  www.slideshare.net/simon/advanced-django/
Debugging Django
Upcoming SlideShare
Loading in...5
×

Debugging Django

29,069

Published on

Full write-up here: http://simonwillison.net/2008/May/22/debugging/

Published in: Technology, Business
2 Comments
66 Likes
Statistics
Notes
  • learned lot debug techniques in django, useful
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • better than django.org
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
29,069
On Slideshare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
523
Comments
2
Likes
66
Embeds 0
No embeds

No notes for slide

Debugging Django

  1. 1. Debugging Django Simon Willison DJUGL, 19th May 2008
  2. 2. This talk is not about Test Driven Development http://www.flickr.com/photos/alikaragoz/209296304/
  3. 3. This talk is about Bug Driven Development
  4. 4. http://www.shipmentoffail.com/fails/2008/04/horse-vs-car-fail/
  5. 5. Make the most of the error page Print statements and logging Using the debugger Catching errors in production Abusing the Test Client
  6. 6. The Django error page It’s not just for errors! Trigger it explicitly with “assert False” Show a value with “assert False, variable”
  7. 7. Logging to your console def index(req): print quot;Hello there!quot; [19/May/2008 18:14:39] quot;GET /static/css/img/ djangosite80x15.gif HTTP/1.1quot; 304 0 [19/May/2008 18:14:39] quot;GET /static/css/img/purple- gradient.png HTTP/1.1quot; 304 0 Hello there! [19/May/2008 18:14:47] quot;GET / HTTP/1.1quot; 200 12570
  8. 8. Logging to your console # in settings.py import logging logging.basicConfig( level = logging.DEBUG, format = '%(asctime)s %(levelname)s %(message)s', ) # Anywhere else import logging logging.debug(quot;A log messagequot;)
  9. 9. Logging to a file # in settings.py import logging logging.basicConfig( level = logging.DEBUG, format = '%(asctime)s %(levelname)s %(message)s', filename = '/tmp/dango.log', filemode = 'w' ) # Anywhere else import logging logging.debug(quot;A log messagequot;) $ tail -f /tmp/django.log
  10. 10. Logging the calling context import logging, traceback, pprint def my_buggy_function(arg): context = pprint.pformat(traceback.extract_stack()) logging.debug(context)
  11. 11. Using the debugger import pdb; pdb.set_trace() $ python -i ./manage.py ... ... >>> import pdb; pdb.pm()
  12. 12. Errors in production Two misleadingly-named settings: # Receive 500 error e-mails if not DEBUG ADMINS = ( ('Simon Willison', 'simon@simonwillison.net'), ) # Receive 404 e-mails if SEND_BROKEN_LINK_EMAILS MANAGERS = ( ... ) IGNORABLE_404_ENDS = ('.php', '.cgi')
  13. 13. Errors over XMPP
  14. 14. db-error-log
  15. 15. Custom error middleware class DBLogMiddleware(object): def process_exception(self, request, exception): server_name = socket.gethostname() tb_text = traceback.format_exc() class_name = exception.__class__.__name__ ... # in settings.py MIDDLEWARE_CLASSES = ( ..., 'djangodblog.DBLogMiddleware', )
  16. 16. More useful middleware ProfilerMiddleware See profiler output with url?prof DebugFooter SQL and templates logged in footer
  17. 17. Abusing the test client from django.test.utils import setup_test_environment setup_test_environment() from django.test.client import Client c = Client() r = c.get('/2008/speaking/') print r r.template r.context
  18. 18. Test Driven Development www.djangoproject.com/documentation/testing/ www.slideshare.net/simon/advanced-django/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×