Skip to content

Instantly share code, notes, and snippets.

@ashishtajane
Last active May 13, 2024 02:08
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ashishtajane/6531000f8c830717fe62 to your computer and use it in GitHub Desktop.
Save ashishtajane/6531000f8c830717fe62 to your computer and use it in GitHub Desktop.
Get all URL patterns in django
# Open django shell and do following.
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)
@Holmeron
Copy link

Thanks, nice snippet ! slightly updated it for my current version of django (4.2)

from django.urls import get_resolver

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

show_urls(get_resolver().url_patterns)

@aliyuldashev
Copy link

i have just copy pasted this code to my views but i got circular import error. Here my code
app.views.py


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

show_urls(get_resolver().url_patterns)

config.urls.py

from django.contrib import admin
from django.urls import path, include
from .router import Router
urlpatterns = [
    path(f'{Router[ "abstracts" ][ "base" ]}', include("abstracts.urls")),
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment