简体   繁体   中英

Flask get 'next' parameters for a 'POST' request

There is a login function which accepts the data from a POST request. But in this case the "next" query parameter is ignored. What is the alternative for accessing the "next" query parameter other than request.args ??

Below given is my login function

@route('/login/', methods=['GET', 'POST'], endpoint='login')
def login(self):
    form = LoginForm()
    if form.validate_on_submit():
        user = check_login(email=request.form['email'], password=request.form['password'])
        if user:
           login_user(user)
           return redirect(request.args.get('next') or url_for('index'))
        else:
           # something else
    return render_template('login.html', form=form)

Thanks in advance.

You need to use values :

A CombinedMultiDict with the contents of both form and args.

This way, no matter where your next was set, you'll be able to retrieve it:

@route('/login/', methods=['GET', 'POST'], endpoint='login')
def login(self):
    redirect_url = request.values.get('next', url_for('index'))

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