从应用程序的两个不同页面访问一条烧瓶路径

时间:2018-06-22 05:15:35

标签: python html flask

我正在制作一个烧瓶应用程序,当用户登录或用户完成注册时,该应用程序会打开主页。但是,我遇到的问题是我试图同时使用“ POST”方法从注册页面和主屏幕访问“主”路由。

    @app.route("/home", methods=["GET","POST"])
    def login():
        """Logs In"""
        if request.method == 'POST':
            user_name = request.form['username']
            password = request.form['password']

            if check_login(user_name,password,db):
                return render_template("home.html")
        return "These aren't the droids you're looking for"

    def registration():
        """Signs Up"""
        if request.method == 'POST':
            user_name = request.form['username']
            password = request.form['password']

        if register(user_name,password,db):
            return render_template("home.html")
        else:
            return "Not successful"

从我的newuser.html模板调用注册方法,并从login.html调用登录名。不幸的是,我从newuser.html收到一个错误, “无法为端点'registration'构建URL。您是说'static'吗?”。

我的猜测是烧瓶不允许我采用一种方法使用多种方法。有什么解决方法?

1 个答案:

答案 0 :(得分:2)

您应该将代码重新组织为类似的内容:

@app.route("/home", methods=["GET"])
def home():
    return render_template("home.html")

@app.route("/login", methods=["POST"])
def login():
    """Logs In"""
    if request.method == 'POST':
        user_name = request.form['username']
        password = request.form['password']

        if check_login(user_name,password,db):
            return redirect(url_for('home'))
    return "These aren't the droids you're looking for"

@app.route("/registration", methods=["POST"])
def registration():
    """Signs Up"""
    if request.method == 'POST':
        user_name = request.form['username']
        password = request.form['password']

    if register(user_name,password,db):
        return return redirect(url_for('home'))
    else:
        return "Not successful"

home()仅显示您的HTML表单(登录和注册)

login()+ registration()是具有两种不同途径的两种不同方法,如果表单成功,它将重定向到您的主页