You are viewing a read-only archive of the Blogs.Harvard network. Learn more.

django urls: list a project’s url names

Below is quick hack to pull out an array of all the url names in a project.

The script iterates through the “urlpatterns” defined in your settings file as the ROOT_URLCONF.

From the “python shell” prompt:

from django.conf import settings

def load_url_pattern_names(patterns):
    """Retrieve a list of urlpattern names"""
    global URL_NAMES
    for pat in patterns:
        if pat.__class__.__name__ == 'RegexURLResolver':            # load patterns from this RegexURLResolver
        elif pat.__class__.__name__ == 'RegexURLPattern':           # load name from this RegexURLPattern
            if is not None and not in URL_NAMES:
    return URL_NAMES

root_urlconf = __import__(settings.ROOT_URLCONF)        # access the root file
print load_url_pattern_names(root_urlconf.urls.urlpatterns)   # access the "urlpatterns" from the ROOT_URLCONF

There’s probably an easier way to this, but I needed the url names for an error checking script.

Published by


Notes while working in a large academic organization.

2 thoughts on “django urls: list a project’s url names”

Comments are closed.