forms.py
class SomeForm(forms.Form):
CHOICES = (('a','a'),
('b','b'),
('c','c'),
('d','d'),)
picked = forms.MultipleChoiceField(choices=CHOICES, widget=forms.CheckboxSelectMultiple())
views.py
def some_view(request):
if request.method == 'POST':
form = SomeForm(request.POST)
if form.is_valid():
picked = form.cleaned_data.get('picked')
# do something with your results
else:
form = SomeForm
return render_to_response('some_template.html', {'form':form },
context_instance=RequestContext(request))
some_template.html
<form method='post'>
{{ form.as_p }}
<input type='submit' value='submit'>
</form>
results:
explanation:
choices:
The first element in each tuple is the
actual value to be stored. The second
element is the human-readable name for
the option.
getting selected boxes:
form.cleaned_data.get('picked')
will result in a list of the 'actual values'. For example, if I replaced the # do something with your results
with print picked
you see:
[u'a', u'c']
in your console
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…