15👍
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
20👍
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")
- [Django]-Is there a way to list Django signals?
- [Django]-Copy a database column into another in Django
- [Django]-Django manage.py –no-input . Yes or no?
3👍
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
- [Django]-Django formsets: make first required?
- [Django]-Django 1.5 custom User model error. "Manager isn't available; User has been swapped"
- [Django]-Difference between values() and only()
1👍
Because recent versions of Emacs python mode do support ‘pdbtrack’ functionality by default, all you need is just set up breakpoint in your code this way:
import pdb; pdb.set_trace()
Also, you have to start your Django application devserver from within Emacs shell:
M-x shell
And then, in the shell, start the Django development server:
python ./manage.py runserver
P.S. No need for specific pdb sessions or –noreload flag. Noreload would require you to manually restart your applications and so I do not find this useful for Emacs.
- [Django]-Should I be adding the Django migration files in the .gitignore file?
- [Django]-What is the difference between static files and media files in Django?
- [Django]-Auto-truncating fields at max_length in Django CharFields
0👍
I don’t really know anything about it, but putting “debugging Python with emacs” into Google gave me this page about debugging twisted with emacs, so it seems to be possible.
- [Django]-ImportError: No module named 'django.core.urlresolvers'
- [Django]-Django: Can you tell if a related field has been prefetched without fetching it?
- [Django]-Django stops working with RuntimeError: populate() isn't reentrant
0👍
About the general non-emacs-exclusive way, there is a very nice screencast out there you might be interested in: http://ericholscher.com/blog/2008/aug/31/using-pdb-python-debugger-django-debugging-series-/
The emacs integration described above doesn’t work for me yet. It doesn’t really seem to connect to the running application.
Further I consider this blog post here very interesting: http://web.archive.org/web/20101230072606/http://panela.blog-city.com/python_and_emacs_5_pdb_and_emacs.htm
cu
Roman
- [Django]-Multiple level template inheritance in Jinja2?
- [Django]-Django default_from_email name
- [Django]-Any thoughts on A/B testing in Django based project?