How can I see the current urlpatterns that \"reverse\" is looking in?
I\'m calling reverse in a view with an argument that I think should work, but doesn\'t. Any wa
I tested the other answers in this post and they were either not working with Django 2.X, incomplete or too complex. Therefore, here is my take on this:
from django.conf import settings
from django.urls import URLPattern, URLResolver
urlconf = __import__(settings.ROOT_URLCONF, {}, {}, [''])
def list_urls(lis, acc=None):
if acc is None:
acc = []
if not lis:
return
l = lis[0]
if isinstance(l, URLPattern):
yield acc + [str(l.pattern)]
elif isinstance(l, URLResolver):
yield from list_urls(l.url_patterns, acc + [str(l.pattern)])
yield from list_urls(lis[1:], acc)
for p in list_urls(urlconf.urlpatterns):
print(''.join(p))
This code prints all URLs, unlike some other solutions it will print the full path and not only the last node. e.g.:
admin/
admin/login/
admin/logout/
admin/password_change/
admin/password_change/done/
admin/jsi18n/
admin/r///
admin/auth/group/
admin/auth/group/add/
admin/auth/group/autocomplete/
admin/auth/group//history/
admin/auth/group//delete/
admin/auth/group//change/
admin/auth/group//
admin/auth/user//password/
admin/auth/user/
... etc, etc