Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make ChoiceField readonly in django forms

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 ?

like image 873
Nullpoet Avatar asked Sep 26 '12 18:09

Nullpoet


People also ask

How do you make a field Uneditable in Django?

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.

What is read only in Django?

Instead, django-read-only uses always installed database instrumentation to inspect executed queries and only allow those which look like reads.

How do you make a field not required in Django?

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 ."

What is initial in Django forms?

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.


1 Answers

You can use disabled attribute.

one_or_two = forms.ChoiceField(widget=forms.RadioSelect(attrs={'disabled': 'disabled'}), initial='1') 
like image 167
karthikr Avatar answered Sep 28 '22 05:09

karthikr