@@ -96,10 +96,10 @@ items in the same list raise an error:
96
96
[source,python]
97
97
----
98
98
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" )
101
101
with self.assertRaises(ValidationError):
102
- item = Item(list=list_ , text=' bla' )
102
+ item = Item(list=mylist , text=" bla" )
103
103
item.full_clean()
104
104
----
105
105
====
@@ -116,8 +116,8 @@ overdo it on our integrity constraints:
116
116
def test_CAN_save_same_item_to_different_lists(self):
117
117
list1 = List.objects.create()
118
118
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" )
121
121
item.full_clean() # should not raise
122
122
----
123
123
====
@@ -218,11 +218,11 @@ together:
218
218
[source,python]
219
219
----
220
220
class Item(models.Model):
221
- text = models.TextField(default='' )
221
+ text = models.TextField(default="" )
222
222
list = models.ForeignKey(List, default=None, on_delete=models.CASCADE)
223
223
224
224
class Meta:
225
- unique_together = (' list', ' text' )
225
+ unique_together = (" list", " text" )
226
226
----
227
227
====
228
228
@@ -370,10 +370,10 @@ Now if we change our duplicates test to do a `.save` instead of a
370
370
[source,python]
371
371
----
372
372
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" )
375
375
with self.assertRaises(ValidationError):
376
- item = Item(list=list_ , text=' bla' )
376
+ item = Item(list=mylist , text=" bla" )
377
377
# item.full_clean()
378
378
item.save()
379
379
----
@@ -451,18 +451,17 @@ class ListViewTest(TestCase):
451
451
def test_for_invalid_input_shows_error_on_page(self):
452
452
[...]
453
453
454
-
455
454
def test_duplicate_item_validation_errors_end_up_on_lists_page(self):
456
455
list1 = List.objects.create()
457
- item1 = Item.objects.create(list=list1, text=' textey' )
456
+ Item.objects.create(list=list1, text=" textey" )
458
457
response = self.client.post(
459
- f' /lists/{list1.id}/' ,
460
- data={' text': ' textey'}
458
+ f" /lists/{list1.id}/" ,
459
+ data={" text": " textey"},
461
460
)
462
461
463
462
expected_error = escape("You've already got this in your list")
464
463
self.assertContains(response, expected_error)
465
- self.assertTemplateUsed(response, ' list.html' )
464
+ self.assertTemplateUsed(response, " list.html" )
466
465
self.assertEqual(Item.objects.all().count(), 1)
467
466
----
468
467
====
@@ -610,12 +609,11 @@ class ExistingListItemForm(ItemForm):
610
609
super().__init__(*args, **kwargs)
611
610
self.instance.list = for_list
612
611
613
-
614
612
def validate_unique(self):
615
613
try:
616
614
self.instance.validate_unique()
617
615
except ValidationError as e:
618
- e.error_dict = {' text' : [DUPLICATE_ITEM_ERROR]}
616
+ e.error_dict = {" text" : [DUPLICATE_ITEM_ERROR]}
619
617
self._update_errors(e)
620
618
----
621
619
====
@@ -645,13 +643,15 @@ let's see if we can put this form to work in our view.
645
643
We remove the skip, and while we're at it, we can use our new constant. Tidy.
646
644
647
645
[role="sourcecode"]
648
- .src/lists/tests/test_views.py (ch13l049 )
646
+ .src/lists/tests/test_views.py (ch15l014 )
649
647
====
650
648
[source,python]
651
649
----
652
650
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,
655
655
)
656
656
[...]
657
657
@@ -804,32 +804,37 @@ use Django instead, it seems like we need to bring back our custom
804
804
====
805
805
[source,diff]
806
806
----
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>
833
838
----
834
839
====
835
840
0 commit comments