Home > Net >  How do I request the name of an HTML button using python/flask?
How do I request the name of an HTML button using python/flask?

Time:01-01

Here is the html code I have for my input button.

<input type="submit" name="{{ person.id }}" value="x" >

I'm not sure how to use flask/python to store the name of this button in a variable.

Also, I'm not sure if I'm even using the right attribute. Maybe I should be storing person.id in a different attribute. (btw I want the value to be "x" because I want it to display that character so that it looks like a delete button)

This is the python/flask code that I tried.

button_value = request.form.get('x')

I knew that this was wrong, but I didn't know what else to do.

CodePudding user response:

<input> elements of with type="submit" are only supposed to be buttons that submit the form, not to hold data. If you need to pass person.id without creating a textbox, use the hidden attribute, like this:

<input name="x" value="{{ person.id }}" hidden>

Also, the value attribute is supposed to hold the value, not the name of the field (person.id in your example). Instead, you get <input>s by their name attribute. Because of this, the name attribute should be a constant, while the value attribute should change to whatever value is needed. To get the example input I used above, use this flask code:

input_value = request.form.get('x')

(Note: in real applications, use more descriptive namess, such as "id" or "user_id")

  • Related