mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
[5.0.x] Added missing pycon directives in various docs.
Backport of 718b32c691 from main
This commit is contained in:
@@ -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']
|
||||
|
||||
|
||||
Reference in New Issue
Block a user