Skip to content

Commit 5e0a7ed

Browse files
committed
fix code formatting
1 parent 998fefe commit 5e0a7ed

File tree

1 file changed

+51
-46
lines changed

1 file changed

+51
-46
lines changed

chapter_15_advanced_forms.asciidoc

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ items in the same list raise an error:
9696
[source,python]
9797
----
9898
def test_duplicate_items_are_invalid(self):
99-
list_ = List.objects.create()
100-
Item.objects.create(list=list_, text='bla')
99+
mylist = List.objects.create()
100+
Item.objects.create(list=mylist, text="bla")
101101
with self.assertRaises(ValidationError):
102-
item = Item(list=list_, text='bla')
102+
item = Item(list=mylist, text="bla")
103103
item.full_clean()
104104
----
105105
====
@@ -116,8 +116,8 @@ overdo it on our integrity constraints:
116116
def test_CAN_save_same_item_to_different_lists(self):
117117
list1 = List.objects.create()
118118
list2 = List.objects.create()
119-
Item.objects.create(list=list1, text='bla')
120-
item = Item(list=list2, text='bla')
119+
Item.objects.create(list=list1, text="bla")
120+
item = Item(list=list2, text="bla")
121121
item.full_clean() # should not raise
122122
----
123123
====
@@ -218,11 +218,11 @@ together:
218218
[source,python]
219219
----
220220
class Item(models.Model):
221-
text = models.TextField(default='')
221+
text = models.TextField(default="")
222222
list = models.ForeignKey(List, default=None, on_delete=models.CASCADE)
223223
224224
class Meta:
225-
unique_together = ('list', 'text')
225+
unique_together = ("list", "text")
226226
----
227227
====
228228

@@ -370,10 +370,10 @@ Now if we change our duplicates test to do a `.save` instead of a
370370
[source,python]
371371
----
372372
def test_duplicate_items_are_invalid(self):
373-
list_ = List.objects.create()
374-
Item.objects.create(list=list_, text='bla')
373+
mylist = List.objects.create()
374+
Item.objects.create(list=mylist, text="bla")
375375
with self.assertRaises(ValidationError):
376-
item = Item(list=list_, text='bla')
376+
item = Item(list=mylist, text="bla")
377377
# item.full_clean()
378378
item.save()
379379
----
@@ -451,18 +451,17 @@ class ListViewTest(TestCase):
451451
def test_for_invalid_input_shows_error_on_page(self):
452452
[...]
453453
454-
455454
def test_duplicate_item_validation_errors_end_up_on_lists_page(self):
456455
list1 = List.objects.create()
457-
item1 = Item.objects.create(list=list1, text='textey')
456+
Item.objects.create(list=list1, text="textey")
458457
response = self.client.post(
459-
f'/lists/{list1.id}/',
460-
data={'text': 'textey'}
458+
f"/lists/{list1.id}/",
459+
data={"text": "textey"},
461460
)
462461
463462
expected_error = escape("You've already got this in your list")
464463
self.assertContains(response, expected_error)
465-
self.assertTemplateUsed(response, 'list.html')
464+
self.assertTemplateUsed(response, "list.html")
466465
self.assertEqual(Item.objects.all().count(), 1)
467466
----
468467
====
@@ -610,12 +609,11 @@ class ExistingListItemForm(ItemForm):
610609
super().__init__(*args, **kwargs)
611610
self.instance.list = for_list
612611
613-
614612
def validate_unique(self):
615613
try:
616614
self.instance.validate_unique()
617615
except ValidationError as e:
618-
e.error_dict = {'text': [DUPLICATE_ITEM_ERROR]}
616+
e.error_dict = {"text": [DUPLICATE_ITEM_ERROR]}
619617
self._update_errors(e)
620618
----
621619
====
@@ -645,13 +643,15 @@ let's see if we can put this form to work in our view.
645643
We remove the skip, and while we're at it, we can use our new constant. Tidy.
646644

647645
[role="sourcecode"]
648-
.src/lists/tests/test_views.py (ch13l049)
646+
.src/lists/tests/test_views.py (ch15l014)
649647
====
650648
[source,python]
651649
----
652650
from lists.forms import (
653-
DUPLICATE_ITEM_ERROR, EMPTY_ITEM_ERROR,
654-
ExistingListItemForm, ItemForm,
651+
DUPLICATE_ITEM_ERROR,
652+
EMPTY_ITEM_ERROR,
653+
ExistingListItemForm,
654+
ItemForm,
655655
)
656656
[...]
657657
@@ -804,32 +804,37 @@ use Django instead, it seems like we need to bring back our custom
804804
====
805805
[source,diff]
806806
----
807-
@@ -13,13 -14,26 +14,26 @@@
808-
<div class="col-lg-6 text-center">
809-
<h1 class="display-1 mb-4">{% block header_text %}{% endblock %}</h1>
810-
- <form method="POST" action="{% block form_action %}{% endblock %}">
811-
- {{ form.text }}
812-
+ <form
813-
+ class="was-validated" <1>
814-
+ method="POST"
815-
+ action="{% block form_action %}{% endblock %}"
816-
+ >
817-
{% csrf_token %}
818-
+ <input
819-
+ id="id_text"
820-
+ name="text"
821-
+ class="form-control form-control-lg {% if form.errors %}is-invalid{% endif %}" <2>
822-
+ placeholder="Enter a to-do item"
823-
+ value="{{ form.text.value }}"
824-
+ aria-describedby="id_text_feedback"
825-
+ required
826-
+ />
827-
{% if form.errors %}
828-
- <div class="invalid-feedback">{{ form.errors.text }}</div>
829-
+ <div class="invalid-feedback">{{ form.errors.text.0 }}</div> <3>
830-
{% endif %}
831-
</form>
832-
</div>
807+
@@ -14,12 +14,25 @@
808+
<div class="row justify-content-center p-5 bg-body-tertiary rounded-3">
809+
<div class="col-lg-6 text-center">
810+
<h1 class="display-1 mb-4">{% block header_text %}{% endblock %}</h1>
811+
-
812+
- <form method="POST" action="{% block form_action %}{% endblock %}" >
813+
- {{ form.text }}
814+
- {% csrf_token %}
815+
+ <form
816+
+ class="was-validated"
817+
+ method="POST"
818+
+ action="{% block form_action %}{% endblock %}"
819+
+ >
820+
+ {% csrf_token %}
821+
+ <input
822+
+ id="id_text"
823+
+ name="text"
824+
+ class="form-control
825+
+ form-control-lg
826+
+ {% if form.errors %}is-invalid{% endif %}"
827+
+ placeholder="Enter a to-do item"
828+
+ value="{{ form.text.value }}"
829+
+ aria-describedby="id_text_feedback"
830+
+ required
831+
+ />
832+
{% if form.errors %}
833+
- <div class="invalid-feedback">{{ form.errors.text }}</div>
834+
+ <div class="invalid-feedback">{{ form.errors.text.0 }}</div>
835+
{% endif %}
836+
</form>
837+
</div>
833838
----
834839
====
835840

0 commit comments

Comments
 (0)