I have checkboxes inside a form. These checkboxes are a list of persons dynamically created since this list depends on who is logged in. Like, for a supervisor view, it will only list your subordinates.
I'm not sure if I'm missing something but the docs and other answers in SO are all saying that you have to have the id of the input in order to retrieve the form data but in my case, I used the person's ID in the list to come up with the name of the checkbox element. So my checkbox names are like input-{{person_id}}
Now in the post method, I'm not sure how to retrieve the checkboxes in the form. Tried using self.__dict__ but I can't seem to find anything that I can use
link1 - this specifies the name
link2 - this also says that the ID should be pre-determined
link3 - docs also say that if the parameter is not included, an empty list would returned
Maybe there is a workaround where I just get all the elements in the form and make them seen in tornado? Using javascript maybe?
I'm mistaken, it seems that if you use the same name for a checkbox element for all the dynamic checkboxes you create, then when you submit it to the tornado web handler, it will get the values of all those checkboxes that have that name.
the values are retrieved by using the get_arguments
Is this how you mean?
HTML
<form>
<input type="checkbox" id="box1" name="Name1" checked>
<label for="box1">Name 1</label>
<input type="checkbox" id="box2" name="Name2">
<label for="box2">Name 2</label>
</form>
jQuery
$("form input[type='checkbox']").each(function() {
console.log($(this).attr('name'))
})
Output
"Name1"
"Name2"
You can loop through the request.body_arguments attribute; it's dict which maps arguments names to lists of values (to support multiple values for individual names).
class MyHandler(tornado.web.RequestHandler):
def post(self):
for key, value in self.request.body_arguments.items():
print(f"key {key} has value: {value}")
...
There are also arguments (holds both query and body args) and query_arguments (holds only query args) attributes; you can check the docs here.