【问题标题】:Stuck in a redirect loop in Flask application卡在 Flask 应用程序中的重定向循环中
【发布时间】:2020-11-14 14:55:47
【问题描述】:

我在我的烧瓶应用程序中使用以下@app.before_request 语句。

Checker = False
@app.before_request
def requireLogin():
    redirect_routes = ['split']
    allowed_routes = ['login', 'signup', 'split']
    #to force user to login and signup page
    if request.endpoint not in redirect_routes and 'username' not in session and Checker == False:
        print('Start')
        return redirect('/split') 
    elif request.endpoint not in allowed_routes and 'username' not in session and Checker == True:
        return redirect('/login')

当我运行烧瓶时,以下两行循环直到我收到重定向错误。 [24/Jul/2020 13:14:01] "[32mGET /split HTTP/1.1[0m" 302 - 并开始

有趣的是,如果我将登录添加到redirect_routes,并将return redirect('/split') 替换为return redirect('/login'),则不会发生此循环,并且应用程序可以正常运行。

下面是/split

@app.route('/split', methods=['GET', 'POST'])
def Splitter():
    if request.method == 'POST':
        print('Stop')
        username = request.form['username']
        password = request.form['password']
        print(username)
        print(password)
        
        return redirect('/split')

    else:
        return render_template('split.html')

如果您有任何解决方案,请告诉我

【问题讨论】:

    标签: python html web flask


    【解决方案1】:

    我想出了解决办法。我需要将我的函数 Splitter 重命名为 split。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-03
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 2010-10-23
      • 1970-01-01
      相关资源
      最近更新 更多