Determine complete Django url configuration


Question

Is there a way to get the complete django url configuration?

For example Django's debugging 404 page does not show included url configs, so this is not the complete configuration.


Answer: Thanks to Alasdair, here is an example script:

import urls

def show_urls(urllist, depth=0):
    for entry in urllist:
        print "  " * depth, entry.regex.pattern
        if hasattr(entry, 'url_patterns'):
            show_urls(entry.url_patterns, depth + 1)

show_urls(urls.urlpatterns)
1
80
12/19/2015 8:16:37 AM

Accepted Answer

Django is Python, so introspection is your friend.

In the shell, import urls. By looping through urls.urlpatterns, and drilling down through as many layers of included url configurations as possible, you can build the complete url configuration.

import urls
urls.urlpatterns

The list urls.urlpatterns contains RegexURLPattern and RegexURLResolver objects.

For a RegexURLPattern object p you can display the regular expression with

p.regex.pattern

For a RegexURLResolver object q, which represents an included url configuration, you can display the first part of the regular expression with

q.regex.pattern

Then use

q.url_patterns

which will return a further list of RegexURLResolver and RegexURLPattern objects.

47
10/12/2016 2:10:16 PM

Django extensions provides a utility to do this as a manage.py command.

pip install django-extensions

Then add django_extensions to your INSTALLED_APPS in settings.py. then from the console just type the following

python manage.py show_urls

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon