确定完整的 Django url 配置 [英] Determine complete Django url configuration

查看:34
本文介绍了确定完整的 Django url 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获得完整的 django url 配置?

Is there a way to get the complete django url configuration?

例如Django的调试404页面没有显示包含的url配置,所以这不是完整的配置.

For example Django's debugging 404 page does not show included url configs, so this is not the complete configuration.

答案:感谢 Alasdair,这是一个示例脚本:

Answer: Thanks to Alasdair, here is an example script:

import urls

def show_urls(urllist, depth=0):
    for entry in urllist:
        print("  " * depth, entry.regex.pattern)
        if hasattr(entry, 'url_patterns'):
            show_urls(entry.url_patterns, depth + 1)

show_urls(urls.urlpatterns)

推荐答案

Django 是 Python,所以内省是你的朋友.

Django is Python, so introspection is your friend.

在 shell 中,导入 urls.通过循环urls.urlpatterns,并尽可能多地钻取包含的url配置层,您可以构建完整的url配置.

In the shell, import urls. By looping through urls.urlpatterns, and drilling down through as many layers of included url configurations as possible, you can build the complete url configuration.

import urls
urls.urlpatterns

列表 urls.urlpatterns 包含 RegexURLPatternRegexURLResolver 对象.

The list urls.urlpatterns contains RegexURLPattern and RegexURLResolver objects.

对于 RegexURLPattern 对象 p 你可以用

For a RegexURLPattern object p you can display the regular expression with

p.regex.pattern

对于一个RegexURLResolver对象q,它代表一个包含的url配置,你可以用

For a RegexURLResolver object q, which represents an included url configuration, you can display the first part of the regular expression with

q.regex.pattern

然后使用

q.url_patterns

这将返回 RegexURLResolverRegexURLPattern 对象的进一步列表.

which will return a further list of RegexURLResolver and RegexURLPattern objects.

这篇关于确定完整的 Django url 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆