5

I code app with Django == 2.2.3

By now i want to get all urls_patterns of all my project and associated view name.

How can i get something like this:

    urls_patterns_view_names_list = [
        ("view_name", "view_name: url_pattern"),
        ...,
        ...,
        ...,
]

I have seen several topics to the question of url patterns but none correspond and do what I want. For the most part the answers are very old and obsessive for my case. That's why I reformulate my question: I want to have the urls patterns and for each url associated the name of his view: How can I do that with Django 2.2.3 on python 3.7?

Mbambadev
  • 729
  • 1
  • 7
  • 26
  • 1
    Possible duplicate of [Django : How can I see a list of urlpatterns?](https://stackoverflow.com/questions/1275486/django-how-can-i-see-a-list-of-urlpatterns) – Sohaib Farooqi Jul 05 '19 at 07:19
  • It not the same case ! Read before answer ... And your topic very very old and not correspond to wath i want to do ..... – Mbambadev Jul 05 '19 at 07:35
  • @NathanIngram If something is old it is not necessarily wrong. The elder people are the most wise ones sometimes. – wencakisa Jul 05 '19 at 07:42
  • Please how to use django-extension in views.py – Mbambadev Jul 05 '19 at 07:43

2 Answers2

7

You can achieve this with django-extensions:

pip install django-extensions

Then in settings.py:

INSTALLED_APPS = [
    ...
    'django_extensions',
    ...
]

And finally in your command line:

./manage.py show_urls


This will result in exactly what you want. From the package docs:

show_urls: Produce a tab-separated list of (url_pattern, view_function, name) tuples for a project.

wencakisa
  • 5,850
  • 2
  • 15
  • 36
  • @NathanIngram You can get the result that you want by using this method. It is up to you whether you would use it or not. *"I want to have the urls patterns and for each url associated the name of his view"* – wencakisa Jul 05 '19 at 07:40
0

It's not directly trivial, but I've written a module, https://github.com/valohai/django-urr, that has utilities for working and traversing Django URL resolvers.

If you end up using it, you can just use

for entry in django_urr.extract_urls():
    print(vars(entry))

– see here for all of the contents of entrys.

AKX
  • 152,115
  • 15
  • 115
  • 172