Debugging Django
Upcoming SlideShare
Loading in...5
×
 

Debugging Django

on

  • 36,456 views

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

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

Statistics

Views

Total Views
36,456
Views on SlideShare
34,859
Embed Views
1,597

Actions

Likes
64
Downloads
515
Comments
2

16 Embeds 1,597

http://techblog.insureme.com.tw 1220
http://www.nitinh.com 200
http://www.slideshare.net 93
http://127.0.0.1:8000 30
http://blog.chedushi.com 19
http://www.techgig.com 18
http://blog.notsobad.cn 7
http://static.slideshare.net 2
https://drive.jolicloud.com 1
http://cache.baiducontent.com 1
http://cache.baidu.com 1
http://www.schoox.com 1
http://surf.googlemashups.com 1
http://www.blogger.com 1
http://feeds2.feedburner.com 1
https://www.linkedin.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • learned lot debug techniques in django, useful
    Are you sure you want to
    Your message goes here
    Processing…
  • better than django.org
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Debugging Django Debugging Django Presentation Transcript

  • 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 Test Client
  • The Django error page It’s not just for errors! Trigger it explicitly with “assert False” Show a value with “assert False, variable”
  • 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
  • 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;)
  • 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
  • Logging the calling context import logging, traceback, pprint def my_buggy_function(arg): context = pprint.pformat(traceback.extract_stack()) logging.debug(context)
  • 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 Willison', 'simon@simonwillison.net'), ) # Receive 404 e-mails if SEND_BROKEN_LINK_EMAILS MANAGERS = ( ... ) IGNORABLE_404_ENDS = ('.php', '.cgi')
  • Errors over XMPP
  • db-error-log
  • 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', )
  • More useful middleware ProfilerMiddleware See profiler output with url?prof DebugFooter SQL and templates logged in footer
  • 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
  • Test Driven Development www.djangoproject.com/documentation/testing/ www.slideshare.net/simon/advanced-django/