miguelgrinberg/microblog

next

drewbutcher opened this issue · 3 comments

Hey Miguel,

I'm following along with you in the microblog and when I get to chapter 5 for some reason I can't get the 'next' to work correctly. The request.args appears to be empty when the post request to login is made.

What is the URL that appears in the address bar when you are seeing the login page in the browser?

It was my fault.. I had an action defined in the form
<form method="POST" action="{{ url_for('session_store') }}">
so even with the URL having the next value
http://127.0.0.1:5000/login?next=%2F
the post call didn't include the next value... I saw it on the printout after cloning your repository.

Here was the print out with the form action defined:
127.0.0.1 - - [31/Jan/2024 21:44:00] "GET / HTTP/1.1" 302 -
127.0.0.1 - - [31/Jan/2024 21:44:00] "GET /login?next=/ HTTP/1.1" 200 -
127.0.0.1 - - [31/Jan/2024 21:44:14] "POST /login HTTP/1.1" 302 -
127.0.0.1 - - [31/Jan/2024 21:44:14] "GET /drewbutcher HTTP/1.1" 200 -

Here was the printout without the form action defined:
127.0.0.1 - - [31/Jan/2024 21:49:15] "GET / HTTP/1.1" 302 -
127.0.0.1 - - [31/Jan/2024 21:49:15] "GET /login?next=/ HTTP/1.1" 200 -
127.0.0.1 - - [31/Jan/2024 21:49:20] "POST /login?next=/ HTTP/1.1" 302 -
127.0.0.1 - - [31/Jan/2024 21:49:20] "GET / HTTP/1.1" 200 -

Can you use somehow add the request.args.get('next') into the form action with url_for?

I guess you can. I'm not sure if it makes sense outside of a login flow, but request.args.get('next') should give you whatever is in the next argument. You can add it to the action URL like this:

<form method="POST" action="{{ url_for('session_store', next=request.args.get('next')) }}">