Can Python process a list/array of checkboxes from a web form?

1,011 views
Skip to first unread message

NealWalters

unread,
Jun 4, 2009, 8:57:45 PM6/4/09
to Google App Engine
Is it possible to pass a list or array of checkbox values from a
webpage to Python?

For example - give all fields the same name like this:

English:<input type="checkbox" name="language" value="English" /><br/>
Spanish:<input type="checkbox" name="language" value="Spanish" /><br/>
Portuguese: <input type="checkbox" name="language" value="Portuguese" /
><br/>

Or am I going to have give every checkbox a different name and write
more code?

I'd like to do something like this:
languages = self.request.get('language')
self.response.out.write("size=" + str(len(languages)) + "<BR>")
for language in languages:
self.response.out.write("language=" + language + "<br/>")
return

and even persist the languages in BigTable as a single list field.

The above seems to be returning the first item with a value, then
enumerating the letters of that language:
size=10
language=P
language=o
language=r
language=t
language=u
language=g
language=u
language=e
language=s
language=e

What I would like to see is (if these two languages were checked):
size=2
language=English
language=Portuguese

Thanks,
Neal Walters



John Tantalo

unread,
Jun 5, 2009, 12:49:12 AM6/5/09
to Google App Engine

NealWalters

unread,
Jun 8, 2009, 11:34:48 AM6/8/09
to Google App Engine
That worked great!
session.languages = self.request.get_all('language')

Database model defines column:
languages = db.StringListProperty() #stores multiple
languages


Now - I have the data stored, and I want to display it back on the
form (the form is spread across 5 pages, and the user can move
backward and forward between the 5 pages). I have stored the data in
a "Session" table and using Session objects to tie it together.

The list of languages might contain for example: English, Spanish,
French.
The form needs to set the checkboxes as "checked" if the appropriate
language is in my list, something like this:

<img src="http://3wcloud.com/images/flags/english.jpg">
&nbsp;&nbsp;
<input type="checkbox" name="language" value="English" />&nbsp;
{% ifequal Session.languages 'English' %}
checked
{% endifequal %}
>English<br />

In the above, the keyword "ifequal" will do the job.

Is there some keyword like "iflistcontains"? Do I have to do a
forloop and test myself?

Thanks,
Neal Walters

NealWalters

unread,
Jun 8, 2009, 11:50:34 AM6/8/09
to Google App Engine
I also tried this with no success:

{% if session.languages['English'] %}
checked
{% endif %}

which gives this error:
raise TemplateSyntaxError, "Could not parse the remainder: %s" %
token[upto:]
TemplateSyntaxError: Could not parse the remainder: ['English']

I guess this doesn't make sense because I have a list, not a
dictionary, of which "English" could just be one potential value.

Please - what will work for this scenario?

Thanks,
Neal

Jeff S (Google)

unread,
Jun 9, 2009, 8:25:01 PM6/9/09
to google-a...@googlegroups.com
Hi Neal,

I haven't tested this but it seems like you could use

if 'English' in session.languages

Happy coding,

Jeff
Reply all
Reply to author
Forward
0 new messages