diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-10-25 12:27:27 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-25 12:27:27 +0200 |
| commit | 718b32c6918037cfc746d7867333d79a3c887a8c (patch) | |
| tree | bf12588ca5af8a567d61a04b10c0879eee288f37 /docs/ref/forms/api.txt | |
| parent | ee104251c403fbac83b8475163ff2ac01c567d25 (diff) | |
Added missing pycon directives in various docs.
Diffstat (limited to 'docs/ref/forms/api.txt')
| -rw-r--r-- | docs/ref/forms/api.txt | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/docs/ref/forms/api.txt b/docs/ref/forms/api.txt index c4df99af7f..7bec9b120b 100644 --- a/docs/ref/forms/api.txt +++ b/docs/ref/forms/api.txt @@ -148,6 +148,8 @@ if validation has side effects, those side effects will only be triggered once. Returns a ``dict`` that maps fields to their original ``ValidationError`` instances. +.. code-block:: pycon + >>> f.errors.as_data() {'sender': [ValidationError(['Enter a valid email address.'])], 'subject': [ValidationError(['This field is required.'])]} @@ -170,6 +172,8 @@ messages in ``Form.errors``. Returns the errors serialized as JSON. +.. code-block:: pycon + >>> f.errors.as_json() {"sender": [{"message": "Enter a valid email address.", "code": "invalid"}], "subject": [{"message": "This field is required.", "code": "required"}]} @@ -325,10 +329,14 @@ Checking which form data has changed Use the ``has_changed()`` method on your ``Form`` when you need to check if the form data has been changed from the initial data. - >>> data = {'subject': 'hello', - ... 'message': 'Hi there', - ... 'sender': 'foo@example.com', - ... 'cc_myself': True} +.. code-block:: pycon + + >>> data = { + ... "subject": "hello", + ... "message": "Hi there", + ... "sender": "foo@example.com", + ... "cc_myself": True, + ... } >>> f = ContactForm(data, initial=data) >>> f.has_changed() False @@ -336,6 +344,8 @@ form data has been changed from the initial data. When the form is submitted, we reconstruct it and provide the original data so that the comparison can be done: +.. code-block:: pycon + >>> f = ContactForm(request.POST, initial=data) >>> f.has_changed() @@ -350,9 +360,12 @@ The ``changed_data`` attribute returns a list of the names of the fields whose values in the form's bound data (usually ``request.POST``) differ from what was provided in :attr:`~Form.initial`. It returns an empty list if no data differs. +.. code-block:: pycon + >>> f = ContactForm(request.POST, initial=data) >>> if f.has_changed(): ... print("The following fields changed: %s" % ", ".join(f.changed_data)) + ... >>> f.changed_data ['subject', 'message'] |
