Debug view: show available named URL patterns

 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

  1. Resolve URLs to view name by UloPe 5 years, 1 month ago
  2. Generic object_detail view with multiple named URL filters by cotton 2 years, 4 months ago
  3. automatic urlpattern creator by sleytr 4 years, 8 months ago
  4. Resolve URLs to view name and args/kwargs by fahhem 3 years, 5 months ago
  5. general-purpose django XMLRC dispatcher by teepark 5 years, 2 months ago

Comments

(Forgotten your password?)