简体   繁体   English

用Emacs调试Django

[英]Django debugging with Emacs

I found a lot of info about how to debug simple Python programs with Emacs. 我发现了很多关于如何使用Emacs调试简单Python程序的信息。 But what if I want to debug a Django application? 但是,如果我想调试Django应用程序怎么办? 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". 我运行开发服务器,我想以某种方式从Emacs附加到进程,然后设置断点等。类似于Visual Studio的“附加到进程”。 How to do that? 怎么做?

This isn't emacs specific, but you can use the Python debugger by adding the following to a Django view function: 这不是特定于emacs的,但您可以通过将以下内容添加到Django视图函数来使用Python调试器:

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 您可以通过交互式shell检查和修改应用程序的状态 - 查看调试器的Python文档,或者查看某些Python调试示例的此链接


If all you need is logging, add the following to your settings.py : 如果您只需要记录,请将以下内容添加到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: 现在,您可以通过将以下内容添加到任何视图函数来将消息记录到/tmp/mylog.log

import logging
logging.debug("Something happened")

Start pdb like this: 像这样启动pdb:

Mx pdb Mx pdb

Then, start the Django development server: 然后,启动Django开发服务器:

python manage.py runserver --noreload

Once you have the (Pdb) prompt, you need to do this: 有了(Pdb)提示后,您需要这样做:

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 只需导航到您想要的行号,然后

Cx SPC Cx SPC

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/ http://code.google.com/p/django-command-extensions/

Once you install that you can run: 安装后,您可以运行:

python manage.py runserver_plus python manage.py runserver_plus

and you will have an interactive AJAX console on your Error page. 您将在Error页面上拥有一个交互式AJAX console (Obviously, be careful with the amount of access people have to this web server when running in that mode.) (显然,在该模式下运行时,请注意人们对此Web服务器的访问量。)

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

You can get Django Extensions by using pip or easy_install: 您可以使用pip或easy_install获取Django Extensions:

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

If you want to install it from source, grab the git repository from GitHub and run setup.py: 如果要从源代码安装它,请从GitHub获取git存储库并运行setup.py:

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

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: 因为最新版本的Emacs python模式默认支持'pdbtrack'功能,所以你只需要在代码中设置断点:

import pdb; pdb.set_trace()

Also, you have to start your Django application devserver from within Emacs shell: 此外,您必须从Emacs shell中启动Django应用程序devserver:

Mx shell Mx shell

And then, in the shell, start the Django development server: 然后,在shell中,启动Django开发服务器:

python ./manage.py runserver

PS No need for specific pdb sessions or --noreload flag. PS无需特定的pdb会话或--noreload标志。 Noreload would require you to manually restart your applications and so I do not find this useful for Emacs. Noreload会要求您手动重启应用程序,因此我发现这对Emacs没有用。

我对此一无所知,但是将“使用emacs调试Python”放到Google中给了我这个关于使用emacs进行调试的页面,所以它似乎是可能的。

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-/ 关于一般非emacs独有的方式,有一个非常好的截屏你可能感兴趣: http//ericholscher.com/blog/2008/aug/31/using-pdb-python-debugger-django-调试-串联/

The emacs integration described above doesn't work for me yet. 上面描述的emacs集成对我来说还不适用。 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 此外,我认为此博客文章非常有趣: http//web.archive.org/web/20101230072606/http : //panela.blog-city.com/python_and_emacs_5_pdb_and_emacs.htm

cu Roman 罗马

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM