猿问

添加一行代码时出现此错误.... AttributeError: 'Flask'

我已将这行代码添加到我的登录路径中:


profile_picture = url_for('static', filename='profile_pics/' + current_user.profile_picture)

现在我的程序返回此错误:


AttributeError: 'Flask' object has no attribute 'login_manager'

这是我的登录路线:


@app.route("/login", methods=["GET", "POST"])

def login():

    session.clear()

    if request.method == "GET":

        return render_template("login.html")

    if request.method == "POST":

        rows = User.query.filter_by(username=request.form.get("username")).first()

        if rows is None or not check_password_hash(rows.password, request.form.get("password")):

            flash('Usuario o contraseña incorrectos')

            return render_template("login.html")

        else:

            profile_picture = url_for('static', filename='profile_pics/' + current_user.profile_picture)

            session["user_id"] = rows.user_id

            return render_template("profile.html", name=rows.name, genre=rows.genre, profile_picture=profile_picture)

我还导入了 LoginManager 包:


from flask_login import LoginManager, login_required, login_user, current_user

我无法注意到为什么我会收到此错误。提前致谢。


牧羊人nacy
浏览 138回答 1
1回答

倚天杖

您需要添加:login = LoginManager(app)在你之后app = Flask(__name__),我将继续:login.login_view = 'login'请参阅https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-v-user-logins了解背景信息
随时随地看视频慕课网APP

相关分类

Python
我要回答