How do you get the actual value of the input
id after you send it in Flask?
form:
<form action="" method="post">
<input id = "number_one" type="text" name="comment">
<input type="submit" value = "comment">
</form>
like, what I am trying to say is when the form is sent (i.e. when you do this):
request.form.get("comment")
the value of the text field is passed. What I can't figure out is how to get the value of the id
.
So, when the form is sent we could then tell from which form the info was coming from, because each form has a unique id
. In this case the id
is number_one
.
So, how do we go about getting the actual literal value of the id
and not the text input?
You can get form data from Flask's request object with the form attribute: from flask import Flask, request app = Flask(__name__) @app. route('/', methods=['GET', 'POST']) def index(): data = request.
To post HTML form data to the server in URL-encoded format using Python, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the Python POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.
You can't. The id
value is not part of the form data set sent by the browser.
If you need to identify the field, you'll have to either add the id to the input
element name, or if the id
is generated by Javascript code, perhaps store the information in an extra hidden field.
Adding the id to the name could be done with a delimiter perhaps:
<input id = "number_one" type="text" name="comment.number_one">
which would require you to loop over all form keys:
for key is request.form:
if key.startswith('comment.'):
id_ = key.partition('.')[-1]
value = request.form[key]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With