Django remote control initial value

How can I declare the initial value of the switch?

form.py

YESNO = ( ('Yes','Yes'), ('No', 'No'), ) class MyForm(forms.Form): like = forms.ChoiceField(widget=forms.RadioSelect, choices=YESNO) 

myhtml.html

 {{form.like}} 

I am trying to put:

 like = forms.ChoiceField(widget=forms.RadioSelect, choices=YESNO, initial={'Yes':'Yes'}) 

when I run my code, my radio button is not selected yet.

+8
source share
4 answers

i put the initial value right in my views.py ...

 def myviews(request): ..... form = MyForm({'like':'Yes'}) ... 
+4
source

You can check the documentation to create a form field .

+3
source

Yes, this should be done in the view, but the syntax of the accepted answer is incorrect, since it will trigger a field check in all fields before submitting the form. Use instead

 def myviews(request): ..... form = MyForm(initial={'like':'Yes'}) ... 
+2
source

this is the way it works initial='N' which is the first element in the tuple. Choices=(('Y','Yes'), ('N','No') rs_field = forms.ChoiceField(choices=Choices),initial='N', widget=forms.RadioSelect)

0
source

All Articles