miguelgrinberg / microblog

The microblogging application developed in my Flask Mega-Tutorial series. This version maps to the 2024 Edition of the tutorial.
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
MIT License
4.56k stars 1.65k forks source link

next #364

Closed drewbutcher closed 8 months ago

drewbutcher commented 10 months ago

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.

miguelgrinberg commented 10 months ago

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

drewbutcher commented 9 months ago

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?

miguelgrinberg commented 9 months ago

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')) }}">