首页 > 解决方案 > 烧瓶,视图装饰器没有效果

问题描述

我的烧瓶应用程序中的视图装饰器遇到了一些问题。它们似乎根本没有效果。这是查看用户是否登录的基本方法:

# Decorator to ensure user must be logged in
def login_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):

        if session.get("user_id") is None:
            return redirect("/signin")

        return f(*args, **kwargs)

    return decorated_function

如果我在没有登录的情况下启动我的服务器并尝试直接访问某个页面, http://127.0.0.1:5000/profile我会得到一个Internal Server Error. 我想我应该被重定向到 /signin 但事实并非如此。

这是我的第二个装饰器:

# Decorator to ensure user is confirmed via email
def check_confirmed(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):

        if isConfirmed() == "no":

            flash("Please enter secrect code sent to the given email address")
            return redirect("/unconfirmed")

        return f(*args, **kwargs)

    return decorated_function

isConfirmed()从我的数据库返回一个字符串并工作。但同样,它并没有将我重定向到/unconfirmed我没有做的事情。

这两个函数都在同一个文件中,我确实 import from functools import wraps. 我不知道我错过了什么,谁能发现错误?

如果需要我的代码中的更多信息,请随时告诉我。

非常感谢,我祝大家新年快乐!

标签: flaskpython-decoratorspython-3.8

解决方案


我需要一只橡皮鸭!

我忘了在我的路由功能下添加@login_required 和@check_confirmed。现在它可以例外地工作。


推荐阅读