Chapter 12. More Advanced Forms
Now let’s look at some more advanced forms usage. We’ve helped our users to avoid blank list items, now let’s help them avoid duplicate items.
This chapter goes into more intricate details of Django’s form validation, and you can consider it optional if you already know all about customising Django forms. If you’re still learning Django, there’s good stuff in here. If you want to skip ahead, that’s OK too. Make sure you take a quick look at the aside on developer stupidity, and the recap on testing views at the end.
Another FT for Duplicate Items
We add a second test method to ItemValidationTest:
functional_tests/test_list_item_validation.py (ch12l001).
deftest_cannot_add_duplicate_items(self):# Edith goes to the home page and starts a new listself.browser.get(self.server_url)self.get_item_input_box().send_keys('Buy wellies\n')self.check_for_row_in_list_table('1: Buy wellies')# She accidentally tries to enter a duplicate itemself.get_item_input_box().send_keys('Buy wellies\n')# She sees a helpful error messageself.check_for_row_in_list_table('1: Buy wellies')error=self.browser.find_element_by_css_selector('.has-error')self.assertEqual(error.text,"You've already got this in your list")
Why have two test methods rather than extending one, or having a new file and class? It’s a judgement call. These two feel closely related; they’re both about validation on the same input field, so it feels right to keep them in the same file. On the other ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access