class MyForm(forms.Form):
CHOICES = (('1', 'one',), ('2', 'two',))
one_or_two = forms.ChoiceField(widget=forms.RadioSelect, initial='1')
def show(request):
form = MyForm()
# render form
How to make the field one_or_two readonly ?
The disabled boolean argument, when set to True , disables a form field using the disabled HTML attribute so that it won't be editable by users. Even if a user tampers with the field's value submitted to the server, it will be ignored in favor of the value from the form's initial data.
Instead, django-read-only uses always installed database instrumentation to inspect executed queries and only allow those which look like reads.
Just add blank=True in your model field and it won't be required when you're using modelforms . "If the model field has blank=True , then required is set to False on the form field. Otherwise, required=True ."
initial is used to change the value of the field in the input tag when rendering this Field in an unbound Form. initial accepts as input a string which is new value of field. The default initial for a Field is empty. Let's check how to use initial in a field using a project.
You can use disabled attribute.
one_or_two = forms.ChoiceField(widget=forms.RadioSelect(attrs={'disabled': 'disabled'}), initial='1')
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