0

我正在 app-engine-path 上开发应用程序。我想用多项选择(用户可接受的语言)制作表格。代码如下所示:

语言设定:

settings.LANGUAGES = ((u"cs", u"Čeština"), (u"en", u"English"))

表格型号:

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)

表单呈现正常(所有语言都有复选框。ID、NAME 都可以。)但是如果我为用户保存一些语言,这些语言不会选中复选框。

用户模型看起来像这样

class User(User):
  #...
  languages = db.StringListProperty()
  #...

并查看:

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)
4

1 回答 1

0

我是这样解决的:

some_view(request):
    ...
    form = UserForm(instance=user, initial={"languages":user.languages})  
    ...
于 2010-08-04T06:09:39.860 回答