In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?

后端 未结 26 1285
-上瘾入骨i
-上瘾入骨i 2020-11-22 04:09

In a Django form, how do I make a field read-only (or disabled)?

When the form is being used to create a new entry, all fields should be enabled - but when the recor

26条回答
  •  萌比男神i
    2020-11-22 04:28

    You can elegantly add readonly in the widget:

    class SurveyModaForm(forms.ModelForm):
        class Meta:
            model  = Survey
            fields = ['question_no']
            widgets = {
            'question_no':forms.NumberInput(attrs={'class':'form-control','readonly':True}),
            }
    

提交回复
热议问题