在添加「我也是」答案的風險中,我發佈了上述提交腳本的修改版本,該腳本提供了一個視圖,列出了項目中所有的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