Reputation: 33
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>
Upvotes: 0
Views: 57
Reputation: 33
the problem was solved by reinstalling the flask app. Thanks for your effort.
Upvotes: 0
Reputation: 2323
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:
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>
Upvotes: 1