Hook the show_url_patterns view function in to your URLconf to get a page which simply lists all of the named URL patterns in your system - useful for if your template developers need a quick reference as to what patterns they can use in the {% url %} tag.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | from django.core import urlresolvers
from django.http import HttpResponse
intro_text = """Named URL patterns for the {% url %} tag
========================================
e.g. {% url pattern-name %}
or {% url pattern-name arg1 %} if the pattern requires arguments
"""
def show_url_patterns(request):
patterns = _get_named_patterns()
r = HttpResponse(intro_text, content_type = 'text/plain')
longest = max([len(pair[0]) for pair in patterns])
for key, value in patterns:
r.write('%s %s\n' % (key.ljust(longest + 1), value))
return r
def _get_named_patterns():
"Returns list of (pattern-name, pattern) tuples"
resolver = urlresolvers.get_resolver(None)
patterns = sorted([
(key, value[0][0][0])
for key, value in resolver.reverse_dict.items()
if isinstance(key, basestring)
])
return patterns
|
More like this
- Generate and render HTML Table by LLyaudet 1 day, 23 hours ago
- My firs Snippets by GutemaG 5 days, 7 hours ago
- FileField having auto upload_to path by junaidmgithub 1 month, 1 week ago
- LazyPrimaryKeyRelatedField by LLyaudet 1 month, 2 weeks ago
- CacheInDictManager by LLyaudet 1 month, 2 weeks ago
Comments
Please login first before commenting.