Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django debugging with Emacs

I found a lot of info about how to debug simple Python programs with Emacs. But what if I want to debug a Django application? I run the development server and I would like to somehow attach to the process from Emacs and then set breakpoints, etc. Similar to Visual Studio's "attach to process". How to do that?

like image 546
ibz Avatar asked Nov 12 '08 08:11

ibz


3 Answers

This isn't emacs specific, but you can use the Python debugger by adding the following to a Django view function:

import pdb; pdb.set_trace()

Now when you run the development server and view the page, your browser will appear to hang or load very slowly - switch over to your console, and you have access to the full debugger. You can inspect AND modify state of your application via an interactive shell - check out the Python documentation for the debugger, or this link for some Python debugging examples


If all you need is logging, add the following to your settings.py:

logging.basicConfig(
    level = logging.DEBUG,
    format = '%(asctime)s %(levelname)s %(message)s',
    filename = '/tmp/mylog.log',
    filemode = 'w'
)

Now you can log messages to /tmp/mylog.log by adding the following to any view function:

import logging
logging.debug("Something happened")
like image 55
Ben Avatar answered Nov 12 '22 08:11

Ben


Start pdb like this:

M-x pdb

Then, start the Django development server:

python manage.py runserver --noreload

Once you have the (Pdb) prompt, you need to do this:

import sys
sys.path.append('/path/to/directory/containing/views.py')

Once you've done this, you should be able to set breakpoints normally. Just navigate to the line number you want, and

C-x SPC

like image 13
Matthew Talbert Avatar answered Nov 12 '22 08:11

Matthew Talbert


Here's something I found last night that will do exactly what you want when the program crashes:

http://code.google.com/p/django-command-extensions/

Once you install that you can run:

python manage.py runserver_plus

and you will have an interactive AJAX console on your Error page. (Obviously, be careful with the amount of access people have to this web server when running in that mode.)

GitHub: https://github.com/django-extensions/django-extensions

You can get Django Extensions by using pip or easy_install:

$ pip install django-extensions or $ easy_install django-extensions

If you want to install it from source, grab the git repository from GitHub and run setup.py:

$ git clone git://github.com/django-extensions/django-extensions.git
$ cd django-extensions
$ python setup.py install

like image 3
Chad Avatar answered Nov 12 '22 08:11

Chad