[英]In Django, how do I trigger pdb to figure out what's causing a 404 when I don't know where to set a trace?
In my Django app, I'm getting a 404 Page Not Found response that looks like it's being caused by some logic in one of my custom template tags -- but I don't know which one. 在我的Django应用中,我收到一个404页面未找到的响应,它看起来像是由于我的一个自定义模板标签中的某些逻辑所致-但我不知道是哪个。
Using Django Debug Toolbar's Request Vars panel, I can see the view causing the 404 is go_back.utils._register_wrapped_view
-- which is how I know it's coming from one of my go_back.utils
template tags. 使用Django Debug Toolbar的Request Vars面板,我可以看到导致404的视图是go_back.utils._register_wrapped_view
,这就是我知道它来自我的go_back.utils
模板标签之一的方式。
Unfortunately, because template tags need to be decorated and thus show up as _registered_wrapped_view
, I can't tell which tag it is, much less where in the tag code the problem happens. 不幸的是,因为模板标签需要修饰,并显示为_registered_wrapped_view
,所以我无法确定它是哪个标签,更不用说标签代码在哪里发生了问题。 (The tag code is a special utility which works with urls and calls resolve
in several places so it's not obvious.) (标记代码是一个特殊的实用程序,可与url一起使用,并在多个位置resolve
调用,因此并不明显。)
Thus I can't use the normal import pdb; pdb.set_trace()
因此,我不能使用普通的import pdb; pdb.set_trace()
import pdb; pdb.set_trace()
approach because I don't know where to set the trace. import pdb; pdb.set_trace()
方法,因为我不知道在哪里设置跟踪。
So how can I get pdb to break when the 404 happens so I can see the stack trace leading to that point? 那么当404发生时如何使pdb中断,以便我可以看到导致该点的堆栈跟踪?
Try using the Django Debug Toolbar , it can intercept redirects, good for when you cant find where to add a breakpoint. 尝试使用Django Debug Toolbar ,它可以拦截重定向,非常适合在找不到添加断点的位置使用。
In settings make sure you have 在设置中,请确保您有
DEBUG_TOOLBAR_CONFIG = {'INTERCEPT_REDIRECTS': True}
Old question, but there is django-pdb which has a post mortem flag for runserver. 老问题了,但是有django-pdb ,它具有runserver的验尸标志。 It doesn't look well maintained (2 years ago is the last commit), but it is simple middleware and updating it shouldn't be too hard. 它看起来维护得不太好(2年前是最后一次提交),但是它是简单的中间件,并且更新它也不应该太难。
I'm not sure if you know this: 我不确定您是否知道这一点:
You can try putting import pdb; pdb.set_trace()
您可以尝试放入import pdb; pdb.set_trace()
import pdb; pdb.set_trace()
in the function which you want to debug and development server. 要调试和开发服务器的函数中的import pdb; pdb.set_trace()
。 When that function triggers it will break into pdb
shell and you can debug your code. 当该函数触发时,它将进入pdb
shell,您可以调试代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.