25

How do I set a field to read-only in a Django form? I know how to disable a field but that's not what I'm looking for. Any help would be appreciated.

Zagorodniy Olexiy
  • 2,132
  • 3
  • 22
  • 47
Foobar
  • 843
  • 1
  • 10
  • 23

5 Answers5

40

You can use the optional attrs parameter when defining the Field. To wit:

somefield = forms.CharField(
    widget=forms.TextInput(attrs={'readonly':'readonly'})
)
2ps
  • 15,099
  • 2
  • 27
  • 47
nael
  • 1,441
  • 19
  • 36
19

In django 1.9 in a Field.disabled attribute available : https://docs.djangoproject.com/en/1.9/ref/forms/fields/#disabled

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.

otherwise

use the widget 'readonly' attribute

class PatientForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
       super(PatientForm, self).__init__(*args, **kwargs)
       self.fields['field'].widget.attrs['readonly'] = True

    class Meta:
        model = Patient
Ahsan
  • 11,516
  • 12
  • 52
  • 79
mtt2p
  • 1,818
  • 1
  • 15
  • 22
  • 3
    The Django documentation is patently false here; in the case of using the disabled attribute, the value is not ignored in favor of the form's initial data; the value is not passed at all. – kloddant Feb 06 '20 at 21:38
  • The `form` data is not saved in the `database` when fields are `disabled`. – Gathide Sep 09 '22 at 07:11
14

In Django 1.9+:

somefield = forms.CharField(disabled=True)
tread
  • 10,133
  • 17
  • 95
  • 170
  • 17
    Important thing to note is the fields will not submit to the server when they are `disabled`. If you want the field data to be posted make it `readonly` – tread Dec 11 '17 at 12:10
  • there is no attribute called "readonly", please propose a solution if you have. --Thanks – Mic Apr 24 '20 at 00:37
  • 1
    Yes `readonly` is not a core field argument. So use the other answers. eg. `field.widget.attrs['readonly'] = True` – tread Apr 24 '20 at 06:46
  • @stephen I think your last answer should be added to the response because it is the one that saved my day :) – AD Progress Nov 03 '21 at 19:40
0

Maybe one of the best way to do it in front side is widget-tweak even more if you use your form in multiple view.

If you don't want to install extra lib you can simply add field manually and use the django input attribut. Like this :

<input 
    name="{{ form.fieldname.name }}" 
    type="{{ form.fieldname.widget.input_type }}" 
    readonly="readonly"
    >
</input>
CallMarl
  • 524
  • 4
  • 15
-1

Here is the only way I get it to work in Js:

function myFieldOnChange() {
    var myField= document.getElementById("myFieldID")
    myField.setAttribute('readonly', true)
LucianoBAF
  • 189
  • 2
  • 6
  • there is 1000 way to set in front... Create HTML input with same name as back input form and submiting the from will also work. `` will work – CallMarl Nov 27 '21 at 11:08
  • @CallMarl, I showed how you can do this dinamically based on any field OnChange event. Notice that this is different than disabling the field, as mentioned on another comment. The way you showed only applies if you want the field to always be readonly. My solution is when you need it to be readonly on demand. Feel free to share the other 999 ways. – LucianoBAF Mar 28 '22 at 17:36