简体   繁体   中英

Flask Login Returning a 302 Status Code After Login (Not Redirecting to Home Page)

When trying to use Flask-Login, and inputting my login information, I keep getting a 302 status code as shown below

"POST /login HTTP/1.1" 302 -

Followed by a 200 status code:

"GET /login HTTP/1.1" 200 -

This code was working fine a while ago, and I have looked across the internet (including StackOverflow) to try and find a solution to this problem, but have not found a solution. Yes, there are posts about a 302 status code when using flask, but they were of no help to me. The code that I am using is below.

@app.route('/login', methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = LoginForm()
    if form.validate_on_submit():
        if form.validate_on_submit():
            user = User.query.filter_by(username=form.username.data).first()
        if user is None or not user.check_password(form.password.data):
            flash('Invalid username or password')
            return redirect(url_for('login'))
        login_user(user, remember=form.remember_me.data)
        next_page = request.args.get('next')
        if not next_page or url_parse(next_page).netloc != '':
            next_page = url_for('index')
        return redirect(next_page)
    return render_template('login.html', title='Sign In', form=form)

and this is what my route for the index looks like:

@app.route('/index')
@app.route('/')
def index():
    return render_template('index.html')

Your code can be simplified a bit. For instance you have this twice: if form.validate_on_submit():

Here is a refactored version of your code. Note that return redirect(url_for('login')) in case of login failure is not required, because you have render_template at the end, which will be run, and also when the page is initially loaded (GET request). Since you are already in the login routine, redirection does not really make sense. All you need is to adjust the flow of execution.

@app.route('/login', methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    
    form = LoginForm()
    
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user is None or not user.check_password(form.password.data):
            flash('Invalid username or password')
        else:
            login_user(user, remember=form.remember_me.data)

            next_page = request.args.get('next')
            if not next_page or url_parse(next_page).netloc != '':
                next_page = url_for('index')
            return redirect(next_page)
    
    return render_template('login.html', title='Sign In', form=form)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM