diff options
| author | Claude Paroz <claude@2xlibre.net> | 2015-07-07 21:06:57 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2015-07-16 19:36:56 +0200 |
| commit | 1ef4aeab403f7fb74c0b9b11fde854fd03afc40c (patch) | |
| tree | b3852ad0aefffcf4fd29aacc19e1ca9529d9cdb9 /django/forms | |
| parent | 1fed8dd715cc734f51c162d4362d34d357925bc4 (diff) | |
Fixed #25078 -- Added support for disabled form fields
Thanks Keryn Knight and Tim Graham for the reviews.
Diffstat (limited to 'django/forms')
| -rw-r--r-- | django/forms/fields.py | 5 | ||||
| -rw-r--r-- | django/forms/forms.py | 7 |
2 files changed, 10 insertions, 2 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py index afbefb64c0..128b6bcbf1 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -70,7 +70,7 @@ class Field(six.with_metaclass(RenameFieldMethods, object)): def __init__(self, required=True, widget=None, label=None, initial=None, help_text='', error_messages=None, show_hidden_initial=False, - validators=[], localize=False, label_suffix=None): + validators=[], localize=False, disabled=False, label_suffix=None): # required -- Boolean that specifies whether the field is required. # True by default. # widget -- A Widget class, or instance of a Widget class, that should @@ -90,11 +90,14 @@ class Field(six.with_metaclass(RenameFieldMethods, object)): # hidden widget with initial value after widget. # validators -- List of additional validators to use # localize -- Boolean that specifies if the field should be localized. + # disabled -- Boolean that specifies whether the field is disabled, that + # is its widget is shown in the form but not editable. # label_suffix -- Suffix to be added to the label. Overrides # form's label_suffix. self.required, self.label, self.initial = required, label, initial self.show_hidden_initial = show_hidden_initial self.help_text = help_text + self.disabled = disabled self.label_suffix = label_suffix widget = widget or self.widget if isinstance(widget, type): diff --git a/django/forms/forms.py b/django/forms/forms.py index 4ee79d13ce..ad9e87cae4 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -386,7 +386,10 @@ class BaseForm(object): # value_from_datadict() gets the data from the data dictionaries. # Each widget type knows how to retrieve its own data, because some # widgets split data over several HTML fields. - value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) + if field.disabled: + value = self.initial.get(name, field.initial) + else: + value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) try: if isinstance(field, FileField): initial = self.initial.get(name, field.initial) @@ -567,6 +570,8 @@ class BoundField(object): widget.is_localized = True attrs = attrs or {} + if self.field.disabled: + attrs['disabled'] = True auto_id = self.auto_id if auto_id and 'id' not in attrs and 'id' not in widget.attrs: if not only_initial: |
