How to get a list of all views in a django app?

Is there a way to get a list of all views in a django app? I have an answer for an answer. All answers show a way to get a list of URLs.

+5
source share
2 answers

Getting a list of all the views of a Django project:

To get all the views present in the Django project, we create the get_all_view_names() function, which takes urlpatterns as input and returns a complete list of the views used in the project as output.

First we import the root_urlconf module using settings.ROOT_URLCONF . Then root_urlconf.urls.urlpatterns will provide us with a list of project urls.

The above list of URLs contains the RegexURLPattern and RegexURLResolver . Accessing .urlpatterns on RegexURLResolver will also give us a list of RegexURLPattern and RegexURLResolver .

The RegexURLPattern object will give us the name of the view that interests us. The callback attribute on it contains the called view. When we pass a string in our URLs, for example, 'foo_app.views.view_name' representing the module path and view function name, or the called view, the callback attribute is set to this. Further access to .func_name will give us the name of the view.

We call the get_all_view_names() function recursively and add the view names received from the RegexURLPattern object to the RegexURLPattern global list.

 from django.conf import settings from django.core.urlresolvers import RegexURLResolver, RegexURLPattern root_urlconf = __import__(settings.ROOT_URLCONF) # import root_urlconf module all_urlpatterns = root_urlconf.urls.urlpatterns # project urlpatterns VIEW_NAMES = [] # maintain a global list def get_all_view_names(urlpatterns): global VIEW_NAMES for pattern in urlpatterns: if isinstance(pattern, RegexURLResolver): get_all_view_names(pattern.url_patterns) # call this function recursively elif isinstance(pattern, RegexURLPattern): view_name = pattern.callback.func_name # get the view name VIEW_NAMES.append(view_name) # add the view to the global list return VIEW_NAMES get_all_view_names(all_urlpatterns) 

Getting a list of all the views in a Django application:

To get a list of all the views present in the Django application, we will use the get_all_view_names() function defined above.

First, we import all the urlpatterns application and pass this list to the get_all_view_names() function.

 from my_app.urls import urlpatterns as my_app_urlpatterns # import urlpatterns of the app my_app_views = get_all_view_names(my_app_urlpatterns) # call the function with app urlpatterns as the argument 

my_app_views gives us a list of all the views presented in my_app Django application.

+10
source

Adding to the Rahul fix, if someone uses Python3 , you need to use __name__ instead of func_name :

 ... view_name = pattern.callback.__name__ ... 

otherwise you will receive the following:

 AttributeError: 'function' object has no attribute 'get_all_view_names' 

(Thanks to scipy-gitbot at https://github.com/scipy/scipy/issues/2101#issuecomment-17027406

Alternatively, if you are not inclined to use global variables, here is what I ended up using:

 all_urlpatterns = __import__(settings.ROOT_URLCONF).urls.urlpatterns detail_views_list = [] def get_all_view_names(urlpatterns): for pattern in urlpatterns: if isinstance(pattern, RegexURLResolver): get_all_view_names(pattern.url_patterns) elif isinstance(pattern, RegexURLPattern): detail_views_list.append(pattern.callback.__name__) get_all_view_names(all_urlpatterns) all_views_list = [] # remove redundant entries and specific ones we don't care about for each in detail_views_list: if each not in "serve add_view change_view changelist_view history_view delete_view RedirectView": if each not in all_views_list: all_views_list.append(each) 

Then you can just all_views_list through all_views_list to get a list of filtered views.

update: March 1, 2018

In Django 2.0, django.core.urlresolvers moves to django.urls . RegexURLPattern and RegexURLResolver renamed to URLPattern and URLResolver . Therefore you should use

 from django.urls import URLResolver, URLPattern 

instead

 from django.core.urlresolvers import RegexURLResolver, RegexURLPattern 

if you are using Django 2.

+5
source

Source: https://habr.com/ru/post/1232893/


All Articles