冒着添加“我也是”答案的风险,我发布了上面提交的脚本的修改版本,使您可以看到一个列出项目中所有URL的视图(按字母顺序美化和排序),以及它们所调用的视图。开发人员工具比生产页面更多。
def all_urls_view(request):
from your_site.urls import urlpatterns
nice_urls = get_urls(urlpatterns)
return render(request, "yourapp/links.html", {"links":nice_urls})
def get_urls(raw_urls, nice_urls=[], urlbase=''):
'''Recursively builds a list of all the urls in the current project and the name of their associated view'''
from operator import itemgetter
for entry in raw_urls:
fullurl = (urlbase + entry.regex.pattern).replace('^','')
if entry.callback:
viewname = entry.callback.func_name
nice_urls.append({"pattern": fullurl,
"location": viewname})
else:
get_urls(entry.url_patterns, nice_urls, fullurl)
nice_urls = sorted(nice_urls, key=itemgetter('pattern'))
return nice_urls
和模板:
<ul>
{% for link in links %}
<li>
{{link.pattern}} ----- {{link.location}}
</li>
{% endfor%}
</ul>
如果您想花大价钱,可以为带有变量的任何正则表达式的输入框呈现列表,这些正则表达式可以将变量传递到视图(再次作为开发人员工具而不是生产页面)。