0

I have a search bar on my Flask powered website which works well when the user clicks the search button (I am using a JS code with a POST request). However it doesn't work when the user sends the form with an 'enter' click. In those cases the browser loads a link like this /?finder=searchedword.

The finder is the value of the name attribute of the html code used for the form. In order to solve the problem, I have tried to setup a redirect that always redirects to the proper link that should be loaded, but unfortunately using this solution doesn't work. It just simply redirects to the main page "/".

This is how I manage the redirect:

@app.route("/?finder=<tag>")
def search(tag):
    t = tag.replace(' ', '-')
    return redirect("/result/%s/" % (t,), code=301)

My question is what would be the proper technique to handle this type of redirection? This kind of redirection always works for me and I am using it in several apps, therefore I have no idea what is the weak part of the implementation.

rihekopo
  • 3,241
  • 4
  • 34
  • 63

1 Answers1

1

Past the question mark is not actually part of the route but rather is arguments to the page. So Flask is just matching the url "/".

Try something like this:

@app.route("/")
def search():
    if "finder" in request.args:
        t = request.args["finder"].replace(" ", "-")
        return redirect("/result/%s/" % (t,), code=301)
Zags
  • 37,389
  • 14
  • 105
  • 140