summaryrefslogtreecommitdiff
path: root/docs/ref/forms/api.txt
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2023-10-25 12:27:27 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-10-25 12:27:56 +0200
commit415ef34c4c2d4e9416ecf04ddf8cfb33585f1934 (patch)
tree12194fbcd557df1cd3ea5099307da032ad360812 /docs/ref/forms/api.txt
parent8b18e0bb3ba8bb1f51e15487a6d5402853e637ae (diff)
[5.0.x] Added missing pycon directives in various docs.
Backport of 718b32c6918037cfc746d7867333d79a3c887a8c from main
Diffstat (limited to 'docs/ref/forms/api.txt')
-rw-r--r--docs/ref/forms/api.txt21
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']