Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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 将decorator应用于decorator_Python_Google App Engine_Python 2.7_Flask_Decorator - Fatal编程技术网

Python 将decorator应用于decorator

Python 将decorator应用于decorator,python,google-app-engine,python-2.7,flask,decorator,Python,Google App Engine,Python 2.7,Flask,Decorator,我已经为我的Flask GAE应用程序编写了两个装饰程序: def login_required(f): @wraps(f) def decorated_function(*args, **kwargs): if (not users.get_current_user()): return redirect(home_url) return f(*args, **kwargs) return decorated_fu

我已经为我的Flask GAE应用程序编写了两个装饰程序:

def login_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if (not users.get_current_user()):
            return redirect(home_url)
        return f(*args, **kwargs)
    return decorated_function

def registration_required():
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if(not is_registered_user(users.get_current_user().user_id())):
            return redirect(user_reg_url)
        return f(*args, **kwargs)
    return decorated_function
对于应用程序中的大多数页面,用户需要同时登录和注册,因此视图处理程序使用这两个装饰程序。比如说,

@app.route(some_page)
@login_required
@registration_required
def some_page_view():
    # whatever code here
我认为,从设计的角度来看,如果
registration\u required
的实现本身被
login\u required
修饰,那么这将更有意义,因为它需要用户登录才能检查他们是否已注册。我试过的是:

@login_required
def registration_required():
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if(not is_registered_user(users.get_current_user().user_id())):
            return redirect(user_reg_url)
        return f(*args, **kwargs)
    return decorated_function
然后它可以单独用于视图处理程序,如下所示:

@app.route(some_page)
@registration_required
def some_page_view():
    # whatever code here

但是,当我尝试时,会出现错误
AttributeError:“Response”对象没有属性“\uuuu name\uuuu”
。我需要做什么特别的事情来装饰烧瓶中的装饰器吗?

最简单的方法是调用要在返回的包装上包装的装饰器:

def registration_required():
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if(not is_registered_user(users.get_current_user().user_id())):
            return redirect(user_reg_url)
        return f(*args, **kwargs)
    return login_required(decorated_function)
这是因为decorator语法只是一种简写:

@decorator
def some_func():
    pass
同:

def some_func():
    pass

some_func = decorator(some_func)
def some_func():
    pass

some_func = decorator2(decorator1(some_func))
以及:

同:

def some_func():
    pass

some_func = decorator(some_func)
def some_func():
    pass

some_func = decorator2(decorator1(some_func))

你真的想装饰装饰人还是更想装饰装饰功能?区别当然是相关的。请修正你发布的代码的缩进。问题是关于如何装饰装饰。我很清楚,可以对一个常规函数应用多个修饰符。我已经修复了缩进。当我从编辑那里抄的时候,我没有意识到它被弄坏了。现在我明白了你想要什么(通过第一个答案;-)。请提供您的代码。你只是说“当我尝试时”,但你没有指定“尝试”时使用的语法。