Django url 调试器
我正在开发一个 Django 应用程序,随着时间的推移,URL 不断增长。我现在有很多这样的视图,由于我做了一些改变,一个视图开始出现故障。当我尝试 GET http://example.com/foo/edit_profile 时,它应该执行一个视图某些视图函数 X
但它执行的是 Y
。某处 url 路由混乱,我无法弄清楚。我使用 django.core.urlresolvers.resolve 方法从 shell 中进行尝试,我可以确认 URL 解析错误。但是,我不知道如何调试并查明问题。
理想情况下,我希望看到诸如“测试了此模式”、“测试了此模式”等内容,直到它最终找到正确的模式,然后我可以查看它解决的位置。我找不到这样的东西。
这不是大型项目的常见问题吗?人们做什么?
更新
我知道系统如何工作以及如何一一查看 URL。这就是我正在努力做的事情。这个问题本质上是在求捷径。
I'm developing a django application and over time, the URLs have grown. I have a lot of them with me now and due to some change I made, one view started to malfunction. When I try to GET http://example.com/foo/edit_profile, it's supposed to execute a view certain view function X
but it's executing Y
instead. Somewhere the url routing is messing up and I can't figure it out. I used the django.core.urlresolvers.resolve
method to try it from the shell and I can confirm that the URL is getting wrongly resolved. However, I don't know how to debug this and pinpoint the problem.
Ideally, I'd like to see something like "tested this pattern", "tested this pattern" etc. till it finally finds the correct one and I can then look around where it resolved. I can't find anything like this.
Isn't this a common problem for larger projects? What do people do?
Update
I know how the system works and how to look through the URLs one by one. That's what I'm trying to do. This question is basically asking for a shortcut.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
运行
您是否已尝试在安装 django_extensions 后
? http://vimeo.com/1720508 - 从 06:58 开始观看。
这应该告诉您尝试 url 解析的顺序。
希望这有帮助
have you already tried to run
after installing django_extensions?
http://vimeo.com/1720508 - watch from 06:58.
This should give you in what order the url resolution is attempted.
Hope this helps
我会注释掉 url.py 中的模式,直到您尝试导航到 foo 时收到 404 错误。如果该模式是一个包含,我会递归并注释掉该 url.py 中的行。最终你会确切地知道哪个模式是匹配的。
然后我将获取它正在调用的视图函数并对其进行硬编码。如果它使用通用视图或微妙的东西,我会使其尽可能明显和直接。此时,您应该知道哪个规则匹配、为什么以及它在视图中执行什么代码。
I would comment out the patterns in your url.py until you get a 404 error when you try to navigate to
foo
. If that pattern is an include, I would recurse down that and comment out lines in that url.py. Eventually you will know exactly which pattern is matching.Then I would take the view function that it is calling and hard code that. If it is using a generic view or something subtle, I'd make it as obvious and direct as possible. At that point, you should know which rule is matching and why and what code it is executing in the view.
您可以假设它从上到下遍历 urlpatterns,并且第一个匹配的将被执行。
正如您知道执行哪个视图 (
Y
) 一样,请考虑一下:Y
在X
之前:Y 的模式与 url 匹配(但是不应该)X
在Y
之前:X的模式与url不匹配(但应该)您能提供一些更明确的URLConf示例吗?我可以给你一个更明确的答案。
You can assume, that it goes through the urlpatterns from top to bottom and the first one that matches will be executed.
As you know which view is executed (
Y
) think about that:Y
is beforeX
: the patterns of Y matches the url (but shouldn't)X
is beforeY
: the patterns of X doesn't match the url (but should)Can you provide some more explicit examples of your URLConf? Than I can give you a more explicit answer.
查看您的 urlconfs,找到哪个 urlpattern 调用您的视图 Y,并查看正则表达式是否比应有的更通用。尝试注释掉导致错误匹配的 urlpattern 并查看它是否正确匹配 X。
通常,这对我来说不是问题,但它确实发生了。始终将更具体的模式放在一般模式之前。使用静态前缀来划分 url 命名空间,以防止错误匹配。
Look at your urlconfs, find which urlpattern invokes your view Y and see if the regexp is more general than it ought to be. Try to comment out the urlpattern which causes the false match and see if it correctly matches X.
Typically, this is not a problem for me, but it does occur. Always keep more specific patterns before general ones. Use static prefixes to divide your url namespace, to prevent false matches.