how can I declare initial value of radio button?
form.py
YESNO = (
('Yes','Yes'),
('No', 'No'),
)
class MyForm(forms.Form):
like = forms.ChoiceField(widget=forms.RadioSelect, choices=YESNO)
myhtml.html
{{form.like}}
i try to put:
like = forms.ChoiceField(widget=forms.RadioSelect, choices=YESNO, initial={'Yes':'Yes'})
when i run my code, my radio button has not yet selected..
Yes, this should be done in the view, but the syntax in the accepted answer is incorrect, as it will trigger field validation on all fields before submitting the form. Use instead:
def myviews(request):
.....
form = MyForm(initial={'like':'Yes'})
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With