Displaying a checkbox/list from database back onto a Django form

2,979 views
Skip to first unread message

NealWalters

unread,
Jun 8, 2009, 7:48:34 PM6/8/09
to Django users
Template/Form looks 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" /
>

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

I stored data into Google/BigTable like this:
session.languages = self.request.get_all('language')

That part works fine.

Now - I have the data stored, and I want to display it back on the
same 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?

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 Walters

NealWalters

unread,
Jun 10, 2009, 10:59:42 AM6/10/09
to Django users
Can someone advice if this is the optimal solution? I didn't get any
feedback yesterday - and must get this working.
I've been doing Python a short time, so not always sure the best
Pythonic approach...

I built an array of objects like this:

class Language(db.Model): #this is never stored in database
name = db.StringProperty()
imageURL = db.StringProperty()
selected = db.BooleanProperty()

languages = [];
languages.append(Language(name="English",imageURL="http://3wcloud.com/
images/flags/english.jpg",selected=True));
languages.append(Language(name="Spanish",imageURL="http://3wcloud.com/
images/flags/portuguese.jpg",selected=False))
languages.append(Language(name="Portuguese",imageURL="http://
3wcloud.com/images/flags/portuguese.jpg",selected=False))
languages.append(Language(name="Spanish",imageURL="http://3wcloud.com/
images/flags/mandarin.jpg",selected=False))


I used db.Model just to use the "strong typing" of the fields. Good
idea or bad idea???

then I pass the languages array to the template...

TEMPLATE:


{% for language in languages %}
<img src="{{language.imageURL}}">&nbsp;&nbsp; <span class="wpcf7-
form-control-wrap language_sp"><span class="wpcf7-checkbox"><span
class="wpcf7-list-item">
<input type="checkbox" name="language" value="{{language.name}}"
{% if language.selected %}
checked
{% endif %}
/>&nbsp;
<span class="wpcf7-list-item-label">{{language.name}}</span></
span></span></span><br />
{% endfor %}

The downside of this technique is that I have moved templates and
graphics information from the template to the Python code. In my
case, I'm doing both, but in the ideal world we should separate the
GUI from the program. Now, if a new language is added, the programmer
must change the python code, as opposed to the graphic guy just
updating the template.

Thanks,
Neal Walters

NealWalters

unread,
Jun 10, 2009, 11:19:43 AM6/10/09
to Django users
Here's the last chunk of code. Before I display the template, I have
to reset the array with the languages stored in the database.
I now default every .Selected variable to False, and this code resets
the ones to True based on the database.

for dblanguage in session.languages:
for arrlanguage in languages:
if arrlanguage.name == dblanguage:
arrlanguage.selected = True

Opinions please? I feel like I had to invent a wheel here, when there
was probably already a wheel or pattern out there somewhere.

Neal

Jashugan

unread,
Jun 10, 2009, 11:22:00 AM6/10/09
to Django users


On Jun 10, 7:59 am, NealWalters <NealWalt...@NealWalters.com> wrote:
> Can someone advice if this is the optimal solution?

It's a hard to help you find a solution when you don't try to state
your problem. Just reading your code, it seems if you want to set a
single image depending on what language was selected. Is that all you
want to do, or do you want to internationalize the text in the forms
as well?

NealWalters

unread,
Jun 10, 2009, 1:48:28 PM6/10/09
to Django users
Your correct, I'm so close to my issue, I assumed everybody would see
it.

A user fills out a form to request us to build a website for them.
We ask them what languages the would like - and we give them a list,
such as English, Spanish, Portuguese, etc...
The flag images are there just for looks.

So I stored the list into my bigtable database. The user goes to page
2 or 3 of a five page form, then goes back to the first page where the
languages were, and now, I need to show the same languages checked
that he user checked when he was originally on page 1.

This is just a data collection form, the internationalization, if they
want multiple languages, will actually be done later when we build the
site or sites for them.

Neal

Jashugan

unread,
Jun 11, 2009, 12:30:52 PM6/11/09
to Django users
On Jun 10, 10:48 am, NealWalters <NealWalt...@NealWalters.com> wrote:
> So I stored the list into my bigtable database.  The user goes to page
> 2 or 3 of a five page form, then goes back to the first page where the
> languages were, and now, I need to show the same languages checked
> that he user checked when he was originally on page 1.

So this seems like a good case for the FormWizard, however, the
FormWizard as it stands right now, doesn't allow you to go back to
previous forms (http://code.djangoproject.com/ticket/9200).

I think there are two ways of doing this:

1. use sessions
2. store each step in the database, which you retrieve when you return
to that step. Once all the steps are complete, you can delete the
steps from the database and create a record. Of course, you'd need a
scrubber script that deletes the partially complete processes after 24
hours of inactivity.
Reply all
Reply to author
Forward
0 new messages