Flask Redirect仅在刷新页面时有效

时间:2018-11-19 20:33:57

标签: python flask

烧瓶重定向未重定向到新页面。在我的索引函数中,登录后重定向不会重定向到用户的主页;但是,刷新页面并检查current_user.authenticated后,它的确会重定向到用户的主页。我在两个if语句中都有相同的完全重定向代码。为什么我登录用户后没有立即重定向?

@login.user_loader
def load_user(user_id):
    user = User()
    username = user.get_username(str(user_id))
    user = User(username = username, id = user_id)
    return user

@app.route("/", methods=['GET', 'POST'])
def index():
    if current_user.is_authenticated:
        print('this redirect works')
        return redirect(url_for('home', username = str(current_user.username))) 
    else:
        username = str(request.form.get('user'))
        password = str(request.form.get('password'))       
        user = User(username = username)
        my_hash = user.get_password(username)
        if user.check_password(my_hash, password):
            my_id = user.get_user_id(username)
            user = User(username = username, id = my_id)
            login_user(user, remember=True)
            print('this redirect doesnt work')
            return redirect(url_for('home', username = str(current_user.username))) 
     return render_template('index.html')

@app.route("/<username>", methods=['GET', 'POST'])
@login_required
def home(username):
    print('here')
    if (str(request.form.get('signout')) == 'true'):
        print('logout')
        logout_user()
        return redirect(url_for('index'))
    return render_template('home.html') 

这是登录用户“ a”后的控制台

Serving Flask app "models" (lazy loading)

Environment: production

WARNING: Do not use the development server in a production environment.

Use a production WSGI server instead.

Debug mode: off

Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)

127.0.0.1 - - [20/Nov/2018 10:11:45] "GET / HTTP/1.1" 200 -

127.0.0.1 - - [20/Nov/2018 10:11:45] "GET /static/css/style.css HTTP/1.1" 200 -

127.0.0.1 - - [20/Nov/2018 10:11:45] "GET /static/scripts/jsx/signin.bundle.js HTTP/1.1" 200 -

127.0.0.1 - - [20/Nov/2018 10:11:46] "GET /favicon.ico HTTP/1.1" 401 -

this redirect doesnt work

127.0.0.1 - - [20/Nov/2018 10:11:49] "POST / HTTP/1.1" 302 -

here

127.0.0.1 - - [20/Nov/2018 10:11:50] "GET /a HTTP/1.1" 200 -

0 个答案:

没有答案