Multi choice form field in Django
Posted
by Dingo
on Stack Overflow
See other posts from Stack Overflow
or by Dingo
Published on 2010-03-26T10:02:39Z
Indexed on
2010/03/26
10:03 UTC
Read the original article
Hit count: 415
Hi! I'am developing application on app-engine-path. I would like to make form with multichoice (acceptably languages for user). Code look like this:
Language settings:
settings.LANGUAGES = ((u"cs", u"Ceština"), (u"en", u"English"))
Form model:
class UserForm(forms.ModelForm):
first_name = forms.CharField(max_length=100)
last_name = forms.CharField(max_length=100)
languages = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=settings.LANGUAGES)
The form is rendered o.k. (all languages have checkbox. IDs, NAMEs is ok.) But if I save some languages for user, those languages don't check checkboxes.
User model look like this
class User(User):
#...
languages = db.StringListProperty()
#...
and view:
def edit_profile(request):
user = request.user
if request.method == 'POST':
form = UserForm(request.POST)
if form.is_valid():
# ...
else:
form = UserForm(instance=user)
data = {"user":user, "form": form}
return render_to_response(request, 'user_profile/user_profile.html', data)
© Stack Overflow or respective owner