I have the following HTML code:
JavaScript
x
15
15
1
<form method="post">
2
<h5>Sports you play:</h5>
3
<input type="checkbox" name="sports_played" value="basketball"> basketball<br>
4
<input type="checkbox" name="sports_played" value="football"> football<br>
5
<input type="checkbox" name="sports_played" value="baseball"> baseball<br>
6
<input type="checkbox" name="sports_played" value="soccer"> tennis<br>
7
<input type="checkbox" name="sports_played" value="mma"> MMA<br>
8
<input type="checkbox" name="sports_played" value="hockey"> hockey<br>
9
10
<br>
11
12
<input class="btn" type="submit">
13
14
</form>
15
And then ideally I would like to have the following python serverside code:
JavaScript
1
5
1
class MyHandler(ParentHandler):
2
def post(self):
3
sports_played = self.request.get('sports_played')
4
#sports_played is a list or array of all the selected checkboxes that I can iterate through
5
I tried doing this by making the HTML sports_played name and array, sports_played[], but that didn’t do anything and right now it just always returns the first selected item.
Is this possible? Really I just don’t want to have to do a self.request.get(‘HTML_item’) for each and every checkbox incase I need to alter the HTML I don’t want to have to change the python.
Thanks!
Advertisement
Answer
The answer is shown in the webapp2 docs for the request object:
JavaScript
1
2
1
self.request.get('sports_played', allow_multiple=True)
2
Alternatively you can use
JavaScript
1
2
1
self.request.POST.getall('sports_played')
2