在添加“我也是”答案的风险中,我发布了上述提交脚本的修改版本,该脚本提供了一个视图,列出了项目中所有的URL,有点美化和按字母顺序排序,他们打电话。更多的是开发者工具而不是生产页面。
def all_urls_view(request):
from your_site.urls import urlpatterns #this import should be inside the function to avoid an import loop
nice_urls = get_urls(urlpatterns) #build the list of urls recursively and then sort it alphabetically
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: #if it points to a view
viewname = entry.callback.func_name
nice_urls.append({"pattern": fullurl,
"location": viewname})
else: #if it points to another urlconf, recur!
get_urls(entry.url_patterns, nice_urls, fullurl)
nice_urls = sorted(nice_urls, key=itemgetter('pattern')) #sort alphabetically
return nice_urls
和模板:
<ul>
{% for link in links %}
<li>
{{link.pattern}} ----- {{link.location}}
</li>
{% endfor%}
</ul>
如果你想获得真正看中的,你可以渲染与任何采取变量的正则表达式的输入框列表传递给视图(再次作为开发人员工具而不是生产页面)。
非常有用的脚本,谢谢 – RichVel 2012-12-09 07:04:44