我想在 Django 表单中设置复选框(BooleanField)的值。这些值是用户的名称,因此是可变的。因此,设置默认值不是一种选择。
表格.py
class UserForm(forms.Form):
user = forms.BooleanField() // value set should be of the name of user
模板.html
{% for user in users %}
{{user_form.as_table}} {{user.name}}
{%endfor%}
视图.py
users = GrabhaloUser.objects.all()
user_form = UserForm(request.POST or None)
if request.method == 'POST':
if(user_form.is_valid()):
selected_users = user_form.cleaned_data['user']
#get_data(request,selected_users,query) //this is some function. Ignore this.
ctx = {
'users' : users,
'user_form' : user_form
}
return render_to_response('dashboard/dashboard.html',ctx, context_instance = RequestContext(request))
我该如何解决这个问题?