根据原文链接:https://segmentfault.com/a/1190000017378464,整理
第一步:先导入项目根的urls.py路径
from ceshiproject import urls
第二步:引入 URLPattern,URLResolver
from django.urls.resolvers import URLPattern,URLResolver
第三步:使用递归的方式,把获取所有路径,例子如下:
def get_all_urls(patterns, pre_fix, result): for item in patterns: print(item.pattern) part = item.pattern.regex.pattern.strip("^$") print(part, 'part') if isinstance(item, URLPattern): result.append(pre_fix + part) else: get_all_urls(item.url_patterns, pre_fix + part, result=result) return result print(get_all_urls(urls.urlpatterns, pre_fix="/", result=[]))