如何在flask中创建两阶段登录页?

2024-04-19 15:45:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须创建两个登录页面。其中一个返回公司生产的所有巧克力和其他返回个别巧克力的细节。你知道吗

我试过这个,但没用。你知道吗

@app.route('/login', methods=['GET','POST']) 
@app.route('/login2', methods=['GET','POST'])
def login():
    error = None
    if request.method =='POST':
        un = request.form['username']
        pw = request.form['password']
        #check if username and password is correct and store the result in check
        if check == "error":
            error = 'Invalid Credentials. Please try again.'
        else:
            return redirect(url_for('login2'))
    return render_template('login.html', error = error)
if __name__ == '__main__':
   app.run(debug = True)
def login2():
    error = None
    if request.method =='POST':
        un = request.form['username']
        pw = request.form['password']
        #check if username and password is correct and store the result in check
        if check=="error":
            error = 'Invalid Credentials. Please try again.'
        else:
            return redirect(url_for('home'))
    return render_template('login2.html', error=error)

但这会产生一个错误,即无法为login2构建url。你是说登录吗? 我使用标准模板登录html页面


Tags: andformappurlreturnifrequesthtml
1条回答
网友
1楼 · 发布于 2024-04-19 15:45:07

路由必须位于它调用的函数的正上方,如下所示:

@app.route('/login', methods=['GET','POST']) 
def login():
    error = None
    if request.method =='POST':
        un = request.form['username']
        pw = request.form['password']
        #check if username and password is correct and store the result in check
        if check == "error":
            error = 'Invalid Credentials. Please try again.'
        else:
            return redirect(url_for('login2'))
    return render_template('login.html', error = error)


@app.route('/login2', methods=['GET','POST'])
def login2():
    error = None
    if request.method =='POST':
        un = request.form['username']
        pw = request.form['password']
        #check if username and password is correct and store the result in check
        if check=="error":
            error = 'Invalid Credentials. Please try again.'
        else:
            return redirect(url_for('home'))
    return render_template('login2.html', error=error)


if __name__ == '__main__':
   app.run(debug = True)

相关问题 更多 >