Alex G.P. Alex G.P. - 22 days ago 6
Python Question

Add query string parameter in Flask POST response

I have page with form which. It is working perfect, after form submitted it is replaced with "thank you" message. Initially form is accessible by url

http://localhost:5000/contact
and after submit it has the same URL. I want that after submit url changed to
http://localhost:5000/contact?aftersubmit
. I.e. add query string parameter on server side.

I know that I ca do it with redirection, but thus I am losing post-submit rendered content. Also I do not want that if user input
http://localhost:5000/contact?aftersubmit
could see post-submit content, i.e. I cannot analyze query string on client side and update HTML. It must be done on server side.

How it could be done?

Answer

Short answer: The client should add the query parameter when submitting the form data (e.g. in the action parameter of the form tag).

Explanation: The server is responding to a request to a particular URL. There is no way for the server to "change the URL" of the request. The only thing the server can do is ask the client to send another request to a different URL by returning a redirect. The problem with this approach, as you mentioned, is that the form data will be lost. You could save the form data using cookies or some similar mechanism, but it's much easier to just have the client submit the form to the correct URL in the first place.