简体   繁体   English

在Django中,当我不知道在哪里设置跟踪时,如何触发pdb找出导致404错误的原因?

[英]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.

相关问题 Django的。 继续在我的图像上获得404,不知道我是否正确设置了路径 - Django. keep getting 404 on my images, don't know if i set up the paths right 当我将Django应用程序从一个系统复制到另一个系统时,如何确定要安装哪些依赖项? - How do I figure out what dependencies to install when I copy my Django app from one system to another? 我不明白 django 的 404 显示 - I don't understand django's 404 display Django-如何确定django正在记录哪个模块? - Django - How do I figure out what module is being logged by django? 假设简单的 Django 蛞蝓类别,但我得到一个 404,我不知道为什么 - Supposed simple Django slug categories, but I get a 404 and I can't figure out why 我不知道如何将 .html 文件之一从测验应用程序连接到 django 应用程序。 (找不到页面 (404)) - I don't know how to connect one of the .html files from the quiz application to the django application. (Page not found (404)) 如何确定我的 Django 应用程序需要什么 AWS RDS 实例? - How do I figure out what AWS RDS instance does my Django application need? 我收到此错误,不知道该怎么办 - I get this error and don't know what to do with it 我不知道去哪里看 - I don't know where to look 我不知道如何在Django中显示表单集 - I don't know how to display formset in django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM