miguelgrinberg / flasky

Companion code to my O'Reilly book "Flask Web Development", second edition.
MIT License
8.52k stars 4.2k forks source link

request.args.get('next') in form.validate_on_submit #529

Closed scku208 closed 2 years ago

scku208 commented 2 years ago

Hi, thanks for writing this great repository.

May I ask the concept about login_required and redirect? in flasky/app/auth/views.py start from line 30:

@auth.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data.lower()).first()
        if user is not None and user.verify_password(form.password.data):
            login_user(user, form.remember_me.data)
            next = request.args.get('next')
            if next is None or not next.startswith('/'):
                next = url_for('main.index')
            return redirect(next)
        flash('Invalid email or password.')
    return render_template('auth/login.html', form=form)

Does the line next = request.args.get('next') always return None because it's within form.validate_on_submit() which should always submit the form by method POST?

Maybe It can use request.form.get('next') with writing some hidden input named next in template/auth/login.html in {{ wtf.quick_form(form) }} to get the value of redirect next.

Thanks.

miguelgrinberg commented 2 years ago

Are you following the book? The next argument is not part of the form, it is a feature of Flask-Login that makes the log in experience better for users.

scku208 commented 2 years ago

Are you following the book? The next argument is not part of the form, it is a feature of Flask-Login that makes the log in experience better for users.

Thanks for the reply, and I am not following the book yet but study in your microblog tutorial. It's my mistake to add action="{{ request.path }}" in the submit form template. Now everything goes well and thank you for being patient with me.