Home > front end >  Flask not redirecting to "next" query string
Flask not redirecting to "next" query string

Time:12-01

Below is the code for my login function (simplified).
I am trying to get the "next" parameter from the url to redirect the user to. But it is not working.

@bp.route('/login', methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('main.dashboard'))

    if request.method == 'POST':
        form_data = request.form
        email = form_data.get('email')
        password = form_data.get('password')
        ... (Assume here that the user logged in)
        login_user(user, remember=True)
        next_page = request.args.get('next')
        if not next_page or url_parse(next_page).netloc != '':
            next_page = url_for('main.dashboard')
        return redirect(next_page)
    return render_template('auth/login.html', title='Login')

In the above code, next_page is always empty. Thank you for the help.

CodePudding user response:

The problem was that the "next" argument is not transferred in the post request. I ended up doing something like this on the login page:

      <!-- Hidden input to store the next argument -->
      <input
          type="hidden"
          name="next"
          value="{{ request.args.get('next', '') }}"
      />

And in the Flask route, I retrieve the next argument from: next_page = request.args.get('next')

Silly mistake, my bad.

  • Related