4

I have the following HTML code:

<form method="post">
              <h5>Sports you play:</h5>
                <input type="checkbox" name="sports_played" value="basketball"> basketball<br>
                <input type="checkbox" name="sports_played" value="football"> football<br>
                <input type="checkbox" name="sports_played" value="baseball"> baseball<br>
                <input type="checkbox" name="sports_played" value="soccer"> tennis<br>
                <input type="checkbox" name="sports_played" value="mma"> MMA<br>
                <input type="checkbox" name="sports_played" value="hockey"> hockey<br>
                
                <br> 

                    <input class="btn" type="submit">

</form>

And then ideally I would like to have the following python server-side code:

class MyHandler(ParentHandler):
    def post(self):
        sports_played = self.request.get('sports_played')
        # sports_played is a list or array of all the selected checkboxes that I can iterate through

I tried doing this by making the HTML sports_played name and array, sports_played[], but that didn't do anything -- it just 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 every checkbox. I don't want to have to change the Python code if I alter the HTML.

3

4 Answers 4

9

The answer is shown in the webapp2 docs for the request object:

self.request.get('sports_played', allow_multiple=True)

Alternatively you can use

self.request.POST.getall('sports_played')
Sign up to request clarification or add additional context in comments.

2 Comments

okay nice, I was looking for some change on the HTML side but I should've looked at the framework. That got it working although according to the docs the allow_multiple param is deprecated and it didn't work for me.
also it is get_all() not getall()
6

(although this answer is not related to this question, but it may help django developers)

In Django, request.POST is a QueryDict object. So you can get all the values as a list:

request.POST.getlist('sports_played')

Comments

5

The name of the inputs should have [] at the end so that they are set to the server as an array. Right now, your multiple checkboxes are being sent to the server as many variables with the same name, so only one is recognized. It should look like this:

<form method="post">
              <h5>Sports you play:</h5>
                <input type="checkbox" name="sports_played[]" value="basketball"> basketball<br>
                <input type="checkbox" name="sports_played[]" value="football"> football<br>
                <input type="checkbox" name="sports_played[]" value="baseball"> baseball<br>
                <input type="checkbox" name="sports_played[]" value="soccer"> tennis<br>
                <input type="checkbox" name="sports_played[]" value="mma"> MMA<br>
                <input type="checkbox" name="sports_played[]" value="hockey"> hockey<br>

                <br> 

                    <input class="btn" type="submit">

</form>

Now, if you select more than one, the values will be sent as an array.

1 Comment

This is a PHP specific answer, the question is about a Python web framework. In HTML forms, you don't need the [] for multiple inputs. It is only in PHP that this is needed to get the framework to recognise multiple inputs.
0

If using Flask, the solution is very similar to the Django and webapp2 solution. You can get a list with the values of all the checkboxes with:

selections = request.form.getlist('sports_played')

There is a related Flask specific question with a similar answer here, but the correct answer there has never been accepted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.