Created
August 31, 2018 17:57
-
-
Save asvechkar/9d1d7e31bc08f7c00c606c6744725444 to your computer and use it in GitHub Desktop.
Django 2 routes list
This file contains 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
# app/management/commands/routes.py | |
from django.core.management import BaseCommand | |
from django.urls import resolvers, URLResolver, URLPattern | |
class Command(BaseCommand): | |
""" | |
Show routes list | |
""" | |
def add_arguments(self, parser): | |
pass | |
def handle(self, *args, **kwargs): | |
urls = resolvers.get_resolver() | |
all_urls = list() | |
def show_urls(urls_list): | |
for url_item in urls_list.url_patterns: | |
if isinstance(url_item, URLResolver): | |
show_urls(url_item) | |
elif isinstance(url_item, URLPattern): | |
all_urls.append(url_item) | |
show_urls(urls) | |
print('-' * 160) | |
print(f'| {"Name":32} | {"Views":60} | {"Url":50} | {"Args":5} |') | |
print('-' * 160) | |
for url in all_urls: # type: URLPattern | |
print(f'| {str(url.name):32} | {url.lookup_str:60} | {str(url.pattern):50} | {str(url.default_args):5} |') | |
print('-' * 160) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment