A simple form with ModelChoiceField displayed as radio buttons (the inherited widget).
I’m using an onchange event to POST, everytime a user selects a radio button:
shipping_choice = ShippingChoiceField(
queryset=ShippingMethods.objects.all(),
empty_label=None,
widget=forms.RadioSelect(attrs={
'class': 'order',
'onchange': '$("#shipping_choice").submit()',
})
)
I need to have the radio that the user selected thought, actually be “selected” when the page reloads.
Adding an extra attr ‘selected’, will not do, ’cause it needs to happen when the user actually has made a choice first.
Forms have an
initialattribute which you can use:Then again, I’m guessing you are using a
ModelForm, in which case you should just be passing the instance:Or maybe you just don’t understand that a form can be bound to data: