我的烧瓶应用程序出现错误。其他语法无效

时间:2020-05-16 10:11:48

标签: flask

if usernamedata is None:
            flash("No user named like this", "danger")
            return render_template("login/login.html")
        else:
            for passwor_data in passwordata:
                if sha256_crypt.verify(password, passwor_data):
                    flash("You are logged in", "success")
                    return redirect(url_for("home")
                else :
                    flash("dfgj", "danger")
                    return render_template("login/login.html")
    eturn render_template("login/login.html")




我第二次出错。无效的语法。该如何纠正?

2 个答案:

答案 0 :(得分:0)

       if usernamedata is None:
            flash("No user named like this", "danger")
            return render_template("login/login.html")
        else:
            for passwor_data in passwordata:
                if sha256_crypt.verify(password, passwor_data):
                    flash("You are logged in", "success")
                    return redirect(url_for("home")
                else :
                    flash("dfgj", "danger")
                    return render_template("login/login.html")
        return render_template("login/login.html")
  1. 检查缩进
  2. 输入错误返回错误

答案 1 :(得分:0)

您的第一个else的缩进程度与初始if的缩进程度不同(请参见下面的正确缩进示例)。

您在第8行的redirect缺少结尾)

您在最后return中有一个错字。

if usernamedata is None:
    flash("No user named like this", "danger")
    return render_template("login/login.html")
else:
    for passwor_data in passwordata:
        if sha256_crypt.verify(password, passwor_data):
            flash("You are logged in", "success")
            return redirect(url_for("home"))  # close the parenthesis!
        else:
            flash("dfgj", "danger")
            return render_template("login/login.html")
return render_template("login/login.html")

从更广泛的意义上讲,您可能想遵循一个教程(示例here)并以更合理的方式(从上述教程中)重写登录功能:

...
@auth.route('/login', methods=['POST'])
def login_post():
    email = request.form.get('email')
    password = request.form.get('password')
    remember = True if request.form.get('remember') else False

    user = User.query.filter_by(email=email).first()

    # check if user actually exists
    # take the user supplied password, hash it, and compare it to the hashed password in database
    if not user or not check_password_hash(user.password, password):
        flash('Please check your login details and try again.')
        return redirect(url_for('auth.login')) # if user doesn't exist or password is wrong, reload the page

    # if the above check passes, then we know the user has the right credentials
    return redirect(url_for('main.profile'))