Created
March 2, 2023 14:46
-
-
Save moskrc/e29723dd09857a2a4d6baab9794012de to your computer and use it in GitHub Desktop.
List all Django urls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
from django.core.management import BaseCommand | |
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) | |
class Command(BaseCommand): | |
def handle(self, *args, **kwargs): | |
for p in list_urls(urlconf.urlpatterns): | |
print('/'+''.join(p)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment