Urlpatterns to HTML?

Hi all

Is there an easy way to output the list of urls in urlpatterns as a html list of links?
It is for use in the developing phase of the project.
I know some require id or str pattern, but maybe there is an easy way…

start with this?

from django.urls import get_resolver
for up in get_resolver().url_patterns:
    print(up)

It did t really get me that far…

1 Like

I use the following just to dump the url tree. Maybe it’ll give you some ideas:

import django.urls.resolvers
def print_urls(patterns, indent=0):
    for u in patterns:
        if u.pattern.describe().startswith("'admin/'"): continue
        if u.pattern.describe().startswith("'__debug__/'"): continue
        if u.pattern.describe().startswith("'favicon.ico'"): continue
        if isinstance(u, django.urls.resolvers.URLResolver):
            print("\t"*indent+str(u.pattern.describe()))
            print_urls(u.url_patterns, indent+1)
        if isinstance(u, django.urls.resolvers.URLPattern):
            print("\t"*indent+str(u.pattern.describe()))

print_urls(django.urls.resolvers.get_resolver().url_patterns)

You’ll probably need to dig into what exactly is available within the pattern object to get what you want.

Thank you. I will try that