App.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="." method="post">
Search: <input type="text" name="search">
<input type="submit" value="Show">
</form>
</body>
</html>
main.py
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/CmpPr')
def cmpP():
return render_template('CmpPr.html')
@app.route('/CmpSpes')
def cmpS():
return render_template('CmpSpes.html')
@app.route('/App', methods=['POST', 'GET'])
def App():
search = request.form['search']
return render_template('output.html', n=search)
@app.route('/Gro')
def Gro():
return render_template('Gro.html')
if __name__ == '__main__':
app.run(debug=True)
I have created multiple html pages
I want to print the message, request from TextBox(above code) and print to another html page
I tried using request.form.get('search') but its returning null
And if I use request.form.get('search', FALSE or TRUE) it returns FALSE or TRUE
I have also used if else loop to specify GET and POST method, still it shows the same error
Can anyone please help me on this
Thank You
Firstly, your form action should point to the view that handle the form data (i.e. /App
):
<form action="/App" method="post">
Secondly, you should only obtain the form data when the request's method is POST
, since you have set method="post"
in the template. Also, you will need to render the App.html that contains the form when request method is GET
:
@app.route('/App', methods=['POST', 'GET'])
def App():
if request.method == 'POST': # get form data when method is POST
search = request.form['search']
return render_template('output.html', n=search)
return render_template('App.html') # when the method is GET, it will render App.html
P.S. The error you got is explained clearly that there isn't a key called search
in the form data.