Skip to content

Instantly share code, notes, and snippets.

@dmwyatt
Last active October 25, 2023 22:07
Show Gist options
  • Save dmwyatt/4ef6a34874a01d9f3ffb to your computer and use it in GitHub Desktop.
Save dmwyatt/4ef6a34874a01d9f3ffb to your computer and use it in GitHub Desktop.
[Pretty print urls] Print indented list of urls configured in a django project #django
def show_urls(urllist: list, indent_depth: int = 0, indent_char: str = " "):
"""
Prints a list of urls for this project.
:param urllist: A list of url patterns. Typically `urlpatterns` imported from `urls.py`
:param indent_depth: How far to indent for each sub-level of urls
:param indent_char: The charcter to use to indent.
"""
for entry in urllist:
print(indent_char * indent_depth, entry.regex.pattern, end="")
if hasattr(entry, "name"):
print(indent_char * indent_depth, "(url name: {})".format(entry.name))
else:
print()
if hasattr(entry, "url_patterns"):
show_urls(entry.url_patterns, indent_depth + 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment