Read-Only Field in Django Form

Foobar picture Foobar · Dec 21, 2016 · Viewed 35.3k times · Source

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.

Answer

nael picture nael · Dec 21, 2016

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

somefield = forms.CharField(
    widget=forms.TextInput(attrs={'readonly':'readonly'})
)