0

I reach to a page which has URL parameters. e.g http://127.0.0.1:5000/?A=ABC123&B=44444&C=hello

When I click on search button, with some search string, with form method = post, it searches but has the same URL, which has values from the previous run. It shows the search result but the parameter still has values from the previous run- http://127.0.0.1:5000/?A=ABC123&B=44444&C=hello

I want to remove the url parameters. After my search, the URL should be http://127.0.0.1:5000/

<form method="POST">
   <input type="text" name="search">
   <button type="submit"><i class="fa fa-search"></i></button>
</form>

I expect the search button should clear the existing URL parameters.

chirag1404
  • 45
  • 7
  • you would have to use targe url in form `
    ` . If you don't use it then it uses current url as target.
    – furas Aug 05 '19 at 17:24

3 Answers3

1

From what I understand, you can do a redirect to the request path which would clear the url params:

@app.route("/search", methods=["POST"])
def search():
    ... get results ...
    redirect(request.path)
ybl
  • 1,510
  • 10
  • 16
1

You have to use target url in action in form

<form action="/" ...>

If you don't use it then form uses current url as target and it keeps arguments in url.

from flask import Flask, request

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    print('args:', request.args)
    print('form:', request.form)
    return '''<form action="/" method="POST">
<input type="text" name="search"/>
<button type="submit">OK</button>
</form>'''

app.run()
furas
  • 134,197
  • 12
  • 106
  • 148
0

With POST the data is not visible in the URL, by design. Checkout this resource GET vs POST and navigate to the bottom of the page's -- Compare GET vs. POST table.