[Fixed]-Initial value in radio button django form

5👍

i put the initial value direct in my views.py

def myviews(request):
  .....
  form = MyForm({'like':'Yes'})
  ...
👤gadss

8👍

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'})
  ...

3👍

you can check documentation forms field initial.

1👍

this is the way it works initial='N' which is the first item in the tuple.

Choices=(('Y','Yes'), ('N','No')
rs_field = forms.ChoiceField(choices=Choices),initial='N', widget=forms.RadioSelect)

Leave a comment