如何在 Django 上列出 urlpatterns(端点)?
我怎样才能看到“反向”正在查找的当前 urlpatterns?
我在一个视图中调用反向,并带有一个我认为应该有效但无效的参数。 有什么方法可以检查那里有什么以及为什么我的模式没有?
How can I see the current urlpatterns that "reverse" is looking in?
I'm calling reverse in a view with an argument that I think should work, but doesn't. Any way I can check what's there and why my pattern isn't?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
如果你想要项目中所有 url 的列表,首先你需要安装 django-extensions
你可以简单地使用命令安装。
有关包的更多信息,请转到 django-extensions
之后,添加
django_extensions 在
settings.py
文件中的INSTALLED_APPS
中,如下所示:urls.py 示例:
然后,在终端中运行任何命令
或
基于配置的示例输出示例urls.py:
有关更多信息,您可以查看文档。
If you want a list of all the urls in your project, first you need to install django-extensions
You can simply install using command.
For more information related to package goto django-extensions
After that, add
django_extensions
inINSTALLED_APPS
in yoursettings.py
file like this:urls.py example:
And then, run any of the command in your terminal
or
Sample output example based on config urls.py:
For more information you can check the documentation.
试试这个:
或者如果你还在使用 Django 1.*:
Try this:
Or if you're still on Django 1.*:
Django >= 2.0 解决方案
我测试了这篇文章中的其他答案,它们要么不适用于 Django 2.X,要么不完整或太复杂。 因此,我对此的看法是:
此代码打印所有 URL,与其他一些解决方案不同,它将打印完整路径,而不仅仅是最后一个节点。 例如:
Django >= 2.0 solution
I tested the other answers in this post and they were either not working with Django 2.X, incomplete or too complex. Therefore, here is my take on this:
This code prints all URLs, unlike some other solutions it will print the full path and not only the last node. e.g.:
Django 1.11、Python 2.7.6
然后粘贴以下代码。
示例输出:
Django 1.11, Python 2.7.6
Then paste following code.
Sample output:
在 Django 3.0 中,这很简单:
要获取特定应用程序的模式,您可以使用以下命令:
In Django 3.0, it's as easy as:
To get the patterns for a particular app, you could use the following:
这是一个快速而肮脏的技巧,无需修改任何设置即可获取所需的信息。
这是小猪后退 @robert 的答案。 虽然正确,但我不想将 django-extensions 作为依赖项,即使只是一秒钟。
Here is a quick and dirty hack to just get the information you need without needing to modify any of your settings.
This is piggy backing off @robert's answer. While correct, I didn't want to have
django-extensions
as a dependency even if it was for just a second.我正在使用下一个命令:
(Python3 + Django 1.10)
用法:
示例输出:
I am using the next command:
(Python3 + Django 1.10)
Usage:
Sample output:
activestate 上有一个食谱
There is a recipe on activestate
我使用一个插件: https://github.com/django-extensions/django-extensions,它有一个 show_urls 命令可以提供帮助。
There's a plugin I use: https://github.com/django-extensions/django-extensions, it has a show_urls command that could help.
只需输入您知道不存在的 url,服务器就会返回一条包含 url 模式列表的错误消息。
例如,如果您正在 http://localhost:8000/something 运行网站,
请输入
http://localhost:8000/something/blahNonsense,您的服务器将返回 url 搜索列表并将其显示在浏览器
Simply type in a url you know does not exist and the server will return an error message with a list of url patterns.
For example, if you're running a site at http://localhost:8000/something
Type in
http://localhost:8000/something/blahNonsense, and your server will return the url search list and display it in the browser
在 python 管理.py shell 中
In python manage.py shell
django 2.0 的极简解决方案
例如,如果您正在寻找installed_apps 的第一个应用程序上的网址,您可以像这样访问它:
Minimalist solution for django 2.0
For instance, if you're looking for an url that's on the first app of installed_apps, you can access it like that:
我扩展了 Seti 的命令 以显示命名空间、所有 url 部分、自动调整列宽、按(命名空间、名称)排序:
https://gist.github.com/andreif/263a3fa6e7c425297ffee09c25f66b20
更新:带有 OrderedDict 的新版本现在位于 django-
I have extended Seti's command to show namespace, all url parts, auto-adjust column widths, sorted by (namespace,name):
https://gist.github.com/andreif/263a3fa6e7c425297ffee09c25f66b20
Update: A new version with OrderedDict now lives in django-????s package: https://github.com/5monkeys/django-bananas/blob/master/bananas/management/commands/show_urls.py
Django 1.8、Python 2.7+
只需在 Shell 中运行这些命令即可。 Python的manage.py shell并执行以下代码。
Django 1.8, Python 2.7+
Just run these commands in your Shell. Python manage.py shell and execute the following code.
这里
your_main_app
是放置 settings.py 文件的应用程序名称Here
your_main_app
is the app name where your settings.py file is placedDjango >= 2.0 列表解决方案
采用@CesarCanassa
Django >= 2.0 List Solution
adopted from @CesarCanassa
@Cesar Canassa 的生成器魔法的又一个改编。 可以将其添加到应用的
yourapp/management/commands/dumpurls.py
目录中,以便可以将其作为management.py中的子命令进行访问
。注意:我添加了一行以确保它仅过滤
yourapp
。 如果需要其他 URL,请相应地更新或删除它。作为
management.py
子命令部署路径:
yourapp/management/commands/dumpurls.py
示例输出
Yet another adaption of @Cesar Canassa 's generator magic. This can be added to the
yourapp/management/commands/dumpurls.py
director of your app so that it'll be accessible as a subcommand inmanagement.py
.note: I added a line to make sure it filters for only
yourapp
. Update or remove it accordingly if additional URLs are desired.As a
management.py
SubcommandDeploy Path:
yourapp/management/commands/dumpurls.py
Sample Output
您可以创建动态导入,使用简单的方法从项目中的每个应用程序收集所有 URL 模式,如下所示:
list_of_all_url_patterns = get_url_patterns()
我最近使用类似的方法创建一个模板标记来显示活动状态导航链接。
You can create a dynamic import to gather all URL Patterns from each application in your project with a simple method like so:
list_of_all_url_patterns = get_url_patterns()
I recently used something like this to create a template tag to show active navigation links.
就我而言,我想列出 api 端点,而不必基本上重写我的 urlpatterns。 您无法将 urlpatterns 导入视图,因为它会导致循环导入。 因此,这里有一个非常好的解决方案,使用
django.urls
中的include
函数。 这允许您列出项目中的任何 urlpatterns 集。复制并粘贴此内容并修复一些内容。 Hacky 解决方案对我来说非常有用。
In my case I want to list api endpoints without having to basically rewrite my urlpatterns. You can't import urlpatterns into the view since it causes a circular import. So here is a pretty good solution using the
include
function fromdjango.urls
. This allows you to list any set of urlpatterns in the project.Copy and paste this and fix up some stuff. Hacky solution that works great for me.
如果您使用 DRF,您可以通过以下方式打印特定路由器的所有 URL 模式从
router.get_urls()
打印 urlpatterns(在 Django 应用程序的urls.py
文件中)。打开您的应用程序
urls.py
并将打印语句添加到文件底部,因此整个文件可能如下所示:然后打印模式如下:
In case you are using DRF, you can print all the URL patterns for a particular router by printing the urlpatterns from
router.get_urls()
(within your Django app'surls.py
file).Open your apps
urls.py
and add the print statement to the bottom of the file, so the whole file might look like this:The patterns are then printed like this: