如何在Django中调试好的方法? [英] How to debug in Django, the good way?
问题描述
所以,我开始学习代码在 Python 和更高版本 Django 。第一次,它很难看到追溯,实际上弄清楚我做错了什么,语法错误在哪里。有一段时间过去了,有一些方式,我想我有一个例程来调试我的Django代码。由于这是在编码经验的早期完成的,所以我坐下来,想知道如何做到这一点是无效的,可以做得更快。我通常会找到并纠正我的代码中的错误,但是我想知道我是否应该做得更快?
我通常只是使用Django给出的调试信息。当事情按照我的想法结束时,我会用语法错误打破代码流,并在流程中查看这一点的变量来确定代码执行的不同于我想要的内容。 / p>
但这可以改进吗?有没有一些很好的工具或更好的方法来调试Django代码?
有很多方法可以做到,但是最直接的是简单地
使用 Python调试器。只需将以下行添加到Django视图函数中:
import pdb; pdb.set_trace()
如果您尝试在浏览器中加载该页面,浏览器将挂起你得到一个提示,对实际的执行代码进行调试。
但是还有其他选项建议(我不会推荐他们):
* return HttpResponse({variable to inspect})
* print {variable to inspect}
* raise异常({variable to inspect})
但Python调试器(pdb)推荐用于所有类型的Python代码。如果您已经进入pdb,您还可以查看 IPDB ,使用 ipython 进行调试。快乐编码。
Seafangs 提供的有用参考:
a href =https://web.archive.org/web/20120910233425/http://v3.mike.tig.as/blog/2010/09/14/pdb =noreferrer>使用Python调试器在Django
So, I started learning to code in Python and later Django. The first times it was hard looking at tracebacks and actually figure out what I did wrong and where the syntax error was. Some time has passed now and some way along the way, I guess I got a routine in debugging my Django code. As this was done early in my coding experience, I sat down and wondered if how I was doing this was ineffective and could be done faster. I usually manage to find and correct the bugs in my code, but I wonder if I should be doing it faster?
I usually just use the debug info Django gives when enabled. When things do end up as I thought it would, I break the code flow a lot with a syntax error, and look at the variables at that point in the flow to figure out, where the code does something other than what I wanted.
But can this be improved? Are there some good tools or better ways to debug your Django code?
There are a bunch of ways to do it, but the most straightforward is to simply use the Python debugger. Just add following line in to a Django view function:
import pdb; pdb.set_trace()
If you try to load that page in your browser, the browser will hang and you get a prompt to carry on debugging on actual executing code.
However there are other options suggested by others (I won't recommend them):
* return HttpResponse({variable to inspect})
* print {variable to inspect}
* raise Exception({variable to inspect})
But the Python Debugger (pdb) is highly recommended for all types of Python code. If you are already into pdb, you'd also want to have a look at IPDB that uses ipython for debugging. Happy Coding.
A useful reference provided by Seafangs : Using the Python debugger in Django
这篇关于如何在Django中调试好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!