41
You can use the optional attrs
parameter when defining the Field
. To wit:
somefield = forms.CharField(
widget=forms.TextInput(attrs={'readonly':'readonly'})
)
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
- [Django]-Numeric for loop in Django templates
- [Django]-Multiple Database Config in Django 1.2
- [Django]-Gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3> django
- [Django]-Skip system checks on Django server in DEBUG mode in Pycharm
- [Django]-Django internationalization language codes
- [Django]-How to use subquery in django?
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>
- [Django]-Is APITest with Query params different then just normal url?
- [Django]-Django 1.8 migrate is not creating tables
- [Django]-Simple guestbook django: __init__() takes 1 positional argument but 2 were given
-1
Here is the only way I get it to work in Js:
function myFieldOnChange() {
var myField= document.getElementById("myFieldID")
myField.setAttribute('readonly', true)
- [Django]-Is there something similar to 'rake routes' in django?
- [Django]-Django: Difference between BASE_DIR and PROJECT_ROOT?
- [Django]-Django: Search form in Class Based ListView