I do not understand very well what you want to achieve, if you want to show several checkboxes and that you can choose only one option, then what you need is to change the Widget to RadioSelect
to use radio buttons and the user can only choose one option:
from django import forms
from django.forms import widgets
class formulario(forms.Form):
r1 = forms.ChoiceField(
choices=RESULTADO_P,
widget=widgets.RadioSelect
)
If you want to use multiple selection, you should use what @toledano indicates and change to MultipleChoiceField
:
from django import forms
class formulario(forms.Form):
r1 = forms.MultipleChoiceField(
choices=RESULTADO_P,
initial='?'
)
I do not know if the options you have created are appropriate, if you are going to switch to multiple selection you may no longer need the ?
option:
RESULTADO_P = (
('a', 'a'),
('b', 'b'),
('c', 'c'),
)
class formulario(forms.Form):
r1 = forms.MultipleChoiceField(choices=RESULTADO_P)
Still, it is possible to continue using the initial
parameter on your form. for example, if you want the options a
and c
to be selected initially, you can pass a list as a parameter:
class formulario(forms.Form):
r1 = forms.MultipleChoiceField(choices=RESULTADO_P, initial=['a', 'c'])
Now, to change the selection to checkbox, you only have to modify the Widget you use for that field and use CheckboxSelectMultiple
:
from django import forms
from django.forms import widgets
class formulario(forms.Form):
r1 = forms.MultipleChoiceField(
choices=RESULTADO_P,
widget=widgets.CheckboxSelectMultiple
)
On the other hand, it would be good to explain what you want to achieve by saying that the result is shown in horizontal form . It occurs to me that you can get it using CSS.