Listing all Django URLs in a project
howto python django
2021-02-16
This will list all registered Django URLs in a project, with their arguments.
from django.conf import settings
from django.urls import URLPattern, URLResolver
urlconf = __import__(settings.ROOT_URLCONF, {}, {}, [''])
def urls(ls, acc=None):
if acc is None:
acc = []
if not ls:
return
l = ls[0]
if isinstance(l, URLPattern):
yield acc + [str(l.pattern)]
elif isinstance(l, URLResolver):
yield from urls(l.url_patterns, acc + [str(l.pattern)])
yield from urls(ls[1:], acc)
for u in urls(urlconf.urlpatterns):
print(str(u))