0

I'm unable to fetch information from my html form to my python code. I checked the code many times but there doesn't seem to be a problem. Please check my code and tell me what's wrong. Thank you.

@app.route("/search",methods=["POST","GET"]) 
def search1():                                #python code
    var=request.form.get("search2")
    sear=str(var)
    print(var,sear)
    return " "
<html>                      <!--html code-->
<head>
    <title>hi there</title>
</head>
<body>
    <h1 style="font-family:verdana; font-style:italic;">Welcome to the book sea !!!....</h1>
    <form action="{{ url_for('search1') }}" method="get" align:"center">
        <input type="text" name="search2" placeholder="enter details">
        <button>search</button>
    </form>
    
</body>
5
  • you return, returns " " maybe you meant to use """ <html>... """. Does the above even run? Can you share logs? Commented Apr 19, 2020 at 9:40
  • app.route(....) is a decorator, So use @app.route(....). Commented Apr 19, 2020 at 9:47
  • You can refer to this code github.com/Shivamseth005/login_using_python_flask, check server.py in this github repository Commented Apr 19, 2020 at 9:53
  • @DevanshSoni sorry, copying mistake. Commented Apr 19, 2020 at 10:29
  • @urban the above code ran perfectly, but instead of printing the value which i entered, it prints "none none". Commented Apr 19, 2020 at 11:00

2 Answers 2

1

Change the "get" in your HTML to "post". Because the way your Flask route is set up, it doesn't allow for a variable to be passed using a get request.

<form action="{{ url_for('search1') }}" method="get" align:"center">

to:

<form action="{{ url_for('search1') }}" method="post" align:"center">

Also you might want to remove or edit align:"center" because it's not proper html. Add it in style="" attribute or remove it.

Also:

  1. Add a route to display the search form
  2. Check if the request is a post method since you're accepting both get and post
  3. You can use var=request.form["search2"] instead of var=request.form.get("search2")

===========================================

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("search.html")

@app.route("/search",methods=["POST","GET"])
def search1():                                #python code
    if request.method == 'POST':
        var=request.form["search2"]
        sear=str(var)
        print(var,sear)
    return " "

if __name__ == "__main__":
    app.run(debug=True)

==== search.html .. should be placed in the templates folder of your project ===

<html>                      <!--html code-->
<head>
    <title>hi there</title>
</head>
<body>
<h1 style="font-family:verdana; font-style:italic;">Welcome to the book sea !!!....</h1>
<form action="{{ url_for('search1') }}" method="post">
    <input type="text" name="search2" placeholder="enter details">
    <button>search</button>
</form>

</body>

</html>
Sign up to request clarification or add additional context in comments.

6 Comments

it didnt work, even after changing method to post it fetches nothing.
@stormcloak it should print out sear=str(var) print(var,sear) in your console.
it prints "none" in my console
@stormcloak I added the HTML. It's working fine on my end. Please update the question with your current code if it's still not working. Don't forget to create the templates folder and place search.html in it with the HTML code above.
after using var=request.form["search2"] it showed "400 bad request" error.
|
0

the problem was solved by reinstalling the flask app. Thanks for your effort.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.