From 62510f01e76ad0526c94ea6d1bc6399c1ddf3df4 Mon Sep 17 00:00:00 2001 From: django-bot Date: Wed, 1 Mar 2023 13:35:43 +0100 Subject: [4.2.x] Fixed #34140 -- Reformatted code blocks in docs with blacken-docs. --- docs/ref/forms/api.txt | 219 ++++++++++++++++++++++++------------------ docs/ref/forms/fields.txt | 102 ++++++++++++-------- docs/ref/forms/renderers.txt | 3 +- docs/ref/forms/validation.txt | 66 +++++++------ docs/ref/forms/widgets.txt | 71 ++++++++------ 5 files changed, 270 insertions(+), 191 deletions(-) (limited to 'docs/ref/forms') diff --git a/docs/ref/forms/api.txt b/docs/ref/forms/api.txt index d91dc90a6b..6ae6c2f121 100644 --- a/docs/ref/forms/api.txt +++ b/docs/ref/forms/api.txt @@ -36,10 +36,12 @@ your :class:`Form` class constructor: .. code-block:: pycon - >>> data = {'subject': 'hello', - ... 'message': 'Hi there', - ... 'sender': 'foo@example.com', - ... 'cc_myself': True} + >>> data = { + ... "subject": "hello", + ... "message": "Hi there", + ... "sender": "foo@example.com", + ... "cc_myself": True, + ... } >>> f = ContactForm(data) In this dictionary, the keys are the field names, which correspond to the @@ -58,7 +60,7 @@ check the value of the form's :attr:`~Form.is_bound` attribute: >>> f = ContactForm() >>> f.is_bound False - >>> f = ContactForm({'subject': 'hello'}) + >>> f = ContactForm({"subject": "hello"}) >>> f.is_bound True @@ -93,10 +95,12 @@ and return a boolean designating whether the data was valid: .. code-block:: pycon - >>> data = {'subject': 'hello', - ... 'message': 'Hi there', - ... 'sender': 'foo@example.com', - ... 'cc_myself': True} + >>> data = { + ... "subject": "hello", + ... "message": "Hi there", + ... "sender": "foo@example.com", + ... "cc_myself": True, + ... } >>> f = ContactForm(data) >>> f.is_valid() True @@ -107,10 +111,12 @@ email address: .. code-block:: pycon - >>> data = {'subject': '', - ... 'message': 'Hi there', - ... 'sender': 'invalid email address', - ... 'cc_myself': True} + >>> data = { + ... "subject": "", + ... "message": "Hi there", + ... "sender": "invalid email address", + ... "cc_myself": True, + ... } >>> f = ContactForm(data) >>> f.is_valid() False @@ -256,7 +262,7 @@ it's not necessary to include every field in your form. For example: .. code-block:: pycon - >>> f = ContactForm(initial={'subject': 'Hi there!'}) + >>> f = ContactForm(initial={"subject": "Hi there!"}) These values are only displayed for unbound forms, and they're not used as fallback values if a particular value isn't provided. @@ -271,10 +277,11 @@ precedence: >>> from django import forms >>> class CommentForm(forms.Form): - ... name = forms.CharField(initial='class') + ... name = forms.CharField(initial="class") ... url = forms.URLField() ... comment = forms.CharField() - >>> f = CommentForm(initial={'name': 'instance'}, auto_id=False) + ... + >>> f = CommentForm(initial={"name": "instance"}, auto_id=False) >>> print(f) Name: Url: @@ -298,15 +305,16 @@ dealing with callables whose return values can change (e.g. ``datetime.now`` or >>> import uuid >>> class UUIDCommentForm(CommentForm): ... identifier = forms.UUIDField(initial=uuid.uuid4) + ... >>> f = UUIDCommentForm() - >>> f.get_initial_for_field(f.fields['identifier'], 'identifier') + >>> f.get_initial_for_field(f.fields["identifier"], "identifier") UUID('972ca9e4-7bfe-4f5b-af7d-07b3aa306334') - >>> f.get_initial_for_field(f.fields['identifier'], 'identifier') + >>> f.get_initial_for_field(f.fields["identifier"], "identifier") UUID('1b411fab-844e-4dec-bd4f-e9b0495f04d0') >>> # Using BoundField.initial, for comparison - >>> f['identifier'].initial + >>> f["identifier"].initial UUID('28a09c59-5f00-4ed9-9179-a3b074fa9c30') - >>> f['identifier'].initial + >>> f["identifier"].initial UUID('28a09c59-5f00-4ed9-9179-a3b074fa9c30') Checking which form data has changed @@ -358,12 +366,13 @@ attribute: .. code-block:: pycon - >>> for row in f.fields.values(): print(row) + >>> for row in f.fields.values(): + ... print(row) ... - >>> f.fields['name'] + >>> f.fields["name"] You can alter the field and :class:`.BoundField` of :class:`Form` instance to @@ -409,10 +418,12 @@ it, you can access the clean data via its ``cleaned_data`` attribute: .. code-block:: pycon - >>> data = {'subject': 'hello', - ... 'message': 'Hi there', - ... 'sender': 'foo@example.com', - ... 'cc_myself': True} + >>> data = { + ... "subject": "hello", + ... "message": "Hi there", + ... "sender": "foo@example.com", + ... "cc_myself": True, + ... } >>> f = ContactForm(data) >>> f.is_valid() True @@ -428,10 +439,12 @@ only the valid fields: .. code-block:: pycon - >>> data = {'subject': '', - ... 'message': 'Hi there', - ... 'sender': 'invalid email address', - ... 'cc_myself': True} + >>> data = { + ... "subject": "", + ... "message": "Hi there", + ... "sender": "invalid email address", + ... "cc_myself": True, + ... } >>> f = ContactForm(data) >>> f.is_valid() False @@ -445,17 +458,19 @@ but ``cleaned_data`` contains only the form's fields: .. code-block:: pycon - >>> data = {'subject': 'hello', - ... 'message': 'Hi there', - ... 'sender': 'foo@example.com', - ... 'cc_myself': True, - ... 'extra_field_1': 'foo', - ... 'extra_field_2': 'bar', - ... 'extra_field_3': 'baz'} + >>> data = { + ... "subject": "hello", + ... "message": "Hi there", + ... "sender": "foo@example.com", + ... "cc_myself": True, + ... "extra_field_1": "foo", + ... "extra_field_2": "bar", + ... "extra_field_3": "baz", + ... } >>> f = ContactForm(data) >>> f.is_valid() True - >>> f.cleaned_data # Doesn't contain extra_field_1, etc. + >>> f.cleaned_data # Doesn't contain extra_field_1, etc. {'cc_myself': True, 'message': 'Hi there', 'sender': 'foo@example.com', 'subject': 'hello'} When the ``Form`` is valid, ``cleaned_data`` will include a key and value for @@ -470,7 +485,8 @@ fields. In this example, the data dictionary doesn't include a value for the ... first_name = forms.CharField() ... last_name = forms.CharField() ... nick_name = forms.CharField(required=False) - >>> data = {'first_name': 'John', 'last_name': 'Lennon'} + ... + >>> data = {"first_name": "John", "last_name": "Lennon"} >>> f = OptionalPersonForm(data) >>> f.is_valid() True @@ -513,10 +529,12 @@ include ``checked`` if appropriate: .. code-block:: pycon - >>> data = {'subject': 'hello', - ... 'message': 'Hi there', - ... 'sender': 'foo@example.com', - ... 'cc_myself': True} + >>> data = { + ... "subject": "hello", + ... "message": "Hi there", + ... "sender": "foo@example.com", + ... "cc_myself": True, + ... } >>> f = ContactForm(data) >>> print(f) @@ -776,9 +794,10 @@ attributes:: from django import forms + class ContactForm(forms.Form): - error_css_class = 'error' - required_css_class = 'required' + error_css_class = "error" + required_css_class = "required" # ... and the rest of your fields here @@ -793,13 +812,13 @@ classes, as needed. The HTML will look something like: ... ...