Multi choice form field in Django
- by Dingo
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)