获取 Django 项目下全部 URL,一个函数轻松搞定
废话不多说,直接上代码。
from django.urls import get_resolver, URLPattern, URLResolver
def get_all_url(resolver=None, pre='/'):
if resolver is None:
resolver = get_resolver()
for r in resolver.url_patterns:
if isinstance(r, URLPattern):
if '<pk>' in str(r.pattern):
continue
yield pre + str(r.pattern).replace('^', '').replace('$', ''), r.name
if isinstance(r, URLResolver):
yield from get_all_url(r, pre + str(r.pattern))
if __name__ == '__main__':
# 获取项目全部 url
for url, name in get_all_url():
print("url='{}' name='{}'".format(url, name))
# 获取某个 app 下的全部 url
# 假设有一个 app 叫 dashboard
# 通过 pre 参数传入相应前缀
for url, name in get_all_url(get_resolver('dashboard.urls')):
print("url='{}' name='{}'".format(url, name))
其中用到的函数就是 get_resolver
,让我们看看源码,这个函数应该怎么用。
# django/urls/resolvers.py
def get_resolver(urlconf=None):
if urlconf is None:
urlconf = settings.ROOT_URLCONF
return _get_cached_resolver(urlconf)
@functools.lru_cache(maxsize=None)
def _get_cached_resolver(urlconf=None):
return URLResolver(RegexPattern(r'^/'), urlconf)
传入参数 urlconf
,参数有个默认值 settings.ROOT_URLCONF
,打印出来是 myproject.urls
,其实就是项目全局下的路由文件。
返回值是 URLResolver
类,这个类就比较复杂了,看了源码也不是很明白应该怎么处理这个类。
# django/urls/resolvers.py
class URLResolver:
# 省略了一堆代码
@cached_property
def urlconf_module(self):
if isinstance(self.urlconf_name, str):
return import_module(self.urlconf_name)
else:
return self.urlconf_name
@cached_property
def url_patterns(self):
# urlconf_module might be a valid set of patterns, so we default to it
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
try:
iter(patterns)
except TypeError as e:
msg = (
"The included URLconf '{name}' does not appear to have any "
"patterns in it. If you see valid patterns in the file then "
"the issue is probably caused by a circular import."
)
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
return patterns
不过还是有一个方法引起了我的注意,毕竟我们要取 url,那跟 url 相关的函数还是要特殊关照一下。
打印一下这个结果,看看有没有发现。
print(get_resolver('myproject.urls').url_patterns)
# 输出
# [
# ...
# <URLPattern 'api/v1/api-doc' [name='api-doc']>,
# <URLResolver <URLPattern list> (api-docs:api-docs) # 'api/v1/drf-api-doc'>]
结果是一个列表,元素有两种,分别是 URLPattern
类和 URLResolver
类。
# django/urls/resolvers.py
class URLPattern:
def __init__(self, pattern, callback, default_args=None, name=None):
self.pattern = pattern
self.callback = callback # the view
self.default_args = default_args or {}
self.name = name
前者可以直接就拿 self.pattern
就是对应 url,而后者再递归调用也同样可以拿到结果。
到这里就可以宣布大功告成了。
关注公众号 AlwaysBeta,各种后端开发技术干货。
题图: Image by Rickson Bejar from Pixabay
参考文章:
https://www.ikaze.cn/article/60
大家都在看:
评论