#๐Ÿ”’ Problem with obtaining user input from get method in flask

14 messages ยท Page 1 of 1 (latest)

analog patio
#

I am trying to accept the user input using get method from an input field in a form, but it returns none and redirects to with the wrong parameter in the url. Can someone help me fix this

@app.route("/search")
def search():
    
    search_query = request.args.get("q")
    # search_results = api.search(search_query)
    # return render_template("search.html", query=search_query, response=search_results)
    
    return f'{search_query}'

# for testing and rewriting index folder: 
@app.route("/index")
def index():
    if request.method == "GET":
        query = request.form.get("search_field")
        redirect(url_for("search", q=query))
        
    return render_template("index.html")```
maiden sierraBOT
#

@analog patio

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

analog patio
#

here is the html part:

      <form class="search-form" method="GET" action="/search">
        <input id="search_field" name="search_field" type="text" class="search-bar" placeholder="Search for Videos" required />
        <button type="submit" class="submit-btn">
          <i class="fa-solid fa-magnifying-glass"></i>
        </button>
      </form>```
#

please ping incase of a reply

honest pine
#

idk bout how flask does it but where i seen an input like that it was included in the data= thing of the request and it was a post

#

i would expect u to have to do a post too and extract the data

#

i would make that request with requests as

requests.request("POST", url, data = {k1: inp1, k2: inp2...})
#

@analog patio not much but all i know

#

wait its only 1 input ye id do that with a get

#

u sure it add it in the param tho?

#

and even if it does u didnt specify anywhere that it has to be "q"

misty grail
maiden sierraBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.