Reputation: 13
I have a basic HTML form which includes a text box and a submit button. Let us say, the user types an input to the input box. I want to append that input to the existing URL of the home page.
Example: The URL of the home page be "localhost/home" and the user inputs "data" into the text box.
On the click of submit button, the URL should append the input and look something like: "localhost/home/data" (upon displaying the result fetched from backend using flask).
So, technically when I click submit, I want to be redirected to "localhost/home/data".
I'm using the below HTML form.
<form action="/home" method="POST">
<input type=text name="name">
<input type=submit name="submit">
</form>
and using this as the flask code snippet:
@app.route('/home', methods=['POST', 'GET'])
def basic():
if request.method == 'POST':
path = (my path to json file)
with open(path) as f:
data = json.load(f)
format_list=data[name]
return format_list
return render_template('temp.html')
How can I get the inputted parameters from the form appended to the URL? Thanks!
Upvotes: 1
Views: 546
Reputation: 19684
When you use form submission with flask, the form is exposed on the flask object as a flask.request.form
python dictionary. From here, you can access what was POSTed to your endpoint:
@app.route('/home', methods=['POST', 'GET'])
def basic():
if request.method == 'POST':
path = request.form['name']
Do note you should be giving your input fields id
attributes:
<input id="name" name="name">
If you want to append this to a path or something of that nature, then you can further look into flask.redirect
.
Upvotes: 0