diff options
| author | Richard Oyudo <ebube.rc@gmail.com> | 2018-03-07 22:58:55 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-03-07 16:59:15 -0500 |
| commit | 564847fc1f493cd81063d7313765bdb4c28e0884 (patch) | |
| tree | 6eeef706796239daf4d123d7d7b074dbbaa34213 /docs | |
| parent | e537bf86f0c876aa1000e06cada4320763d7c7dd (diff) | |
[2.0.x] Fixed #28655 -- Added more examples for customizing widgets in a form.
Backport of 8411e4a8fe98ebac4327ee43446a25873703a5e8 from master
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/ref/forms/widgets.txt | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/docs/ref/forms/widgets.txt b/docs/ref/forms/widgets.txt index 1dc57a1f7e..9eb94410ed 100644 --- a/docs/ref/forms/widgets.txt +++ b/docs/ref/forms/widgets.txt @@ -157,6 +157,25 @@ this, you use the :attr:`Widget.attrs` argument when creating the widget:: url = forms.URLField() comment = forms.CharField(widget=forms.TextInput(attrs={'size': '40'})) +You can also modify a widget in the form definition:: + + class CommentForm(forms.Form): + name = forms.CharField() + url = forms.URLField() + comment = forms.CharField() + + name.widget.attrs.update({'class': 'special'}) + comment.widget.attrs.update(size='40') + +Or if the field isn't declared directly on the form (such as model form fields), +you can use the :attr:`Form.fields` attribute:: + + class CommentForm(forms.ModelForm): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields['name'].widget.attrs.update({'class': 'special'}) + self.fields['comment'].widget.attrs.update(size='40') + Django will then include the extra attributes in the rendered output: >>> f = CommentForm(auto_id=False) |
