Get form input value using python / flask id

How do you get the actual value of the input identifier after sending it to Flask?

the form:

 <form action="" method="post"> <input id = "number_one" type="text" name="comment"> <input type="submit" value = "comment"> </form> 

like, I'm trying to say when the form is submitted (i.e. when you do this):

 request.form.get("comment") 

The value of the text field is passed. I cannot figure out how to get the id value.

So, when the form is submitted, we could then tell what form the information came from, because each form has a unique id . In this case, id number_one .

So, how can we get the actual literal id value, rather than text input?

+4
source share
2 answers

You can not. The id value is not part of the form dataset submitted by the browser.

If you need to identify the field, you need to either add an identifier to the name of the input element, or if the id code is generated by Javascript, perhaps save the information in an additional hidden field.

Adding an identifier to a name can be done using a separator:

 <input id = "number_one" type="text" name="comment.number_one"> 

why you need to iterate over all the keys of the form:

 for key is request.form: if key.startswith('comment.'): id_ = key.partition('.')[-1] value = request.form[key] 
+9
source

There is one way to identify fields as well as several forms .

use this

 <input value = "1" type="hidden" name="my_id"> 

so in your view file you can get this value.

 my_id = request.form.get("my_id","") print my_id 

your conclusion will be

 1 

you can also do this ...

 my_id = request.form.get("my_id","") if my_id == "1": ** for example ** value = request.form.get("any_parameter_of_form","") 
0
source

All Articles