How to make checkboxes in Python

I am trying to use checkboxes in my HTML, return these checkboxes to my python backend, and then increment three counts by clicking on it.

Now my HTML looks like this and works fine:

<form method="post"> <input type="checkbox inline" name="adjective" value="entertaining">Entertaining <input type="checkbox inline" name="adjective" value="informative">Informative <input type="checkbox inline" name="adjective" value="exceptional">Exceptional </form> 

and then on my python backend I have the following:

 def post(self): adjective = self.request.get('adjective ') if adjective : #somehow tell if the entertaining box was checked #increment entertaining counter #do the same for the others 
+6
source share
2 answers

If your form has several checkboxes with the same name attribute, the request when submitting the form will have several values ​​for this name.

Your current code uses Request.get to get the value, but it will only get the first value if there are more than one. Instead, you can get all the values ​​using Request.get_all(name) (in webapp) or Request.get(name, allow_multiple=True) (in webapp2). This will return a (possibly empty) list with all the values ​​for this name.

Here's how you could use it in your code:

 def post(self): adjectives = self.request.get('adjective' allow_multiple=True) for a in adjectives: # increment count self.adjective_count[a] += 1 # or whatever # do more stuff with adjective a, if you want # do other stuff with the request 
+7
source

it wouldn’t be easier to change name = "" and put it the same as the value, so you can ask jsut

IF entertains:

IF informative:

im not a python programmer, just say that you are not

0
source

All Articles