Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法获得具有装饰程序Python和Django的视图的名称列表_Python_Django_Django Views_Python Decorators - Fatal编程技术网

有没有办法获得具有装饰程序Python和Django的视图的名称列表

有没有办法获得具有装饰程序Python和Django的视图的名称列表,python,django,django-views,python-decorators,Python,Django,Django Views,Python Decorators,我有这样一个views.py文件 views.py 我想得到一个拥有装饰师“我的装饰师”的视图列表 例如: views_list_with_my_decorator=["a","b","c","d"] 请帮助我,提前谢谢 decorated = [] def myDecorator(fn): decorated.append(fn) ... 也许吧?但是,只有当它们经过修饰(即导入或以其他方式“运行”…如果它们只是在某个地方的.py文件中闲坐着,它将不起作用)我想你可以对文档

我有这样一个views.py文件

views.py

我想得到一个拥有装饰师“我的装饰师”的视图列表

例如:

views_list_with_my_decorator=["a","b","c","d"]
请帮助我,提前谢谢

decorated = []
def myDecorator(fn):
    decorated.append(fn)
    ...

也许吧?但是,只有当它们经过修饰(即导入或以其他方式“运行”…如果它们只是在某个地方的.py文件中闲坐着,它将不起作用)

我想你可以对文档管理员做一些技巧。以下是我的解决方案。只是为了你的灵感。你可以试试:)

如果要尝试函数是否具有装饰器,请执行以下操作:

print a.decorator == 'my_decorator'
from functools import wraps

def my_decorator(func):
    @wraps(func)
    def wrap_func(*args, **kwargs):
        return func()
    wrap_func.decorator = my_decorator.func_name
    return wrap_func

def my_another_decorator(func):
    @wraps(func)
    def wrap_func(*args, **kwargs):
        return func()
    wrap_func.decorator = my_another_decorator.func_name
    return wrap_func

@my_decorator
def a(request):
    pass

@my_decorator
def b(request):
    rpass

@my_decorator
def c(request):
    pass

@my_decorator
def d(request):
    pass

@my_another_decorator
def e(request):
    pass

func_list_with_my_decorator = [e for e in globals().values() if getattr(e, 'decorator', None)=='my_decorator']

print func_list_with_my_decorator
print a.decorator == 'my_decorator'