Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read-Only Field in Django Form

Tags:

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.

like image 362
Foobar Avatar asked Dec 21 '16 21:12

Foobar


People also ask

How do you make a field read only in Django?

Setting readonly on a widget only makes the input in the browser read-only. Adding a clean_sku which returns instance. sku ensures the field value will not change on form level. This way you can use model's (unmodified save) and avoid getting the field required error.

What is formset in Django?

A formset is a layer of abstraction to work with multiple forms on the same page. It can be best compared to a data grid. Let's say you have the following form: >>> from django import forms >>> class ArticleForm(forms.

What is widget in Django?

A widget is Django's representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. The HTML generated by the built-in widgets uses HTML5 syntax, targeting <! DOCTYPE html> .


1 Answers

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

somefield = forms.CharField(     widget=forms.TextInput(attrs={'readonly':'readonly'}) ) 
like image 175
nael Avatar answered Nov 03 '22 08:11

nael