如何在django应用程序中获取所有视图的列表? [英] how to get a list of all views in a django application?

查看:466
本文介绍了如何在django应用程序中获取所有视图的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在django应用程序中获取所有视图的列表?我已经google了回答。所有答案都显示了一种获取网址列表的方法。

解决方案

获取Django项目的所有视图列表:



要获取Django项目中的所有视图,我们创建一个函数 get_all_view_names(),它需要 urlpatterns 作为输入,并返回在项目中使用的视图的完整列表作为输出。



首先,我们导入 root_urlconf 模块使用 settings.ROOT_URLCONF 。然后, root_urlconf.urls.urlpatterns 将给我们项目的urlpatterns的列表。



上述urlpatterns列表包含 RegexURLPattern RegexURLResolver 对象。在 RegexURLResolver 上访问 .urlpatterns 将进一步向我们提供一个 RegexURLPattern RegexURLResolver 对象。



A RegexURLPattern 对象将给我们我们感兴趣的视图名称。 回调属性包含可调用视图。当我们传递我们的网址中的字符串,如'foo_app.views.view_name'表示模块的路径,视图功能名称或可调用视图,则 callback 属性设置为此。进一步访问 .func_name 将给我们查看名称。



我们调用函数 get_all_view_names()递归,并将从 RegexURLPattern 对象获取的视图名称添加到全局列表 VIEW_NAMES

 从django.conf导入设置
from django.core.urlresolvers import RegexURLResolver,RegexURLPattern

root_urlconf = __import __(settings.ROOT_URLCONF)#import root_urlconf模块
all_urlpatterns = root_urlconf.urls.urlpatterns#project的urlpatterns
VIEW_NAMES = []#维护全局列表

def get_all_view_names(urlpatterns):
全局VIEW_NAMES
在urlpatterns中的模式:
如果isinstance(pattern,RegexURLResolver):
get_all_view_names(pattern.url_patterns )#递归调用此函数
elif isinstance(pattern,RegexURLPattern):
view_name = pattern.callback.f unc_name#获取视图名称
VIEW_NAMES.append(view_name)#将视图添加到全局列表
返回VIEW_NAMES

get_all_view_names(all_urlpatterns)



获取Django应用程序中所有视图的列表:



要获取Django应用程序中存在的所有视图的列表,我们将使用上面定义的 get_all_view_names()函数。



我们将首先导入应用程序的所有 urlpatterns ,并将此列表传递给 get_all_view_names()函数。

  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)#调用应用程序的urlpatterns作为参数

my_app_views 为我们提供了 my_app Django应用程序中的所有视图列表。


Is there any way to get a list of all views in an django app? I have googled for answer. All answers shows a way to get list of urls.

解决方案

Getting list of all the views of a Django project:

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

First, we import the root_urlconf module using settings.ROOT_URLCONF. Then root_urlconf.urls.urlpatterns will give us the list of project's urlpatterns.

The above urlpatterns list contains RegexURLPattern and RegexURLResolver objects. Accessing .urlpatterns on a RegexURLResolver will further give us a list of RegexURLPattern and RegexURLResolver objects.

A RegexURLPattern object will give us the view name which we are interested in. The callback attribute on it contains the callable view. When we pass either a string in our urls like 'foo_app.views.view_name' representing the path to a module and a view function name, or a callable view, then callback attribute is set to this. Further accessing .func_name will give us the view name.

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

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's 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 list of all the views in a Django application:

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

We will first import all the urlpatterns of the 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's urlpatterns as the argument

my_app_views gives us the list of all the views present in my_app Django app.

这篇关于如何在django应用程序中获取所有视图的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆