Chapter 16. More Advanced Forms
Let’s look at some more advanced forms usage. We’ve successfully helped our users to avoid blank list items, so now let’s help them to avoid duplicate items as well.
Our validation constraint so far has been about preventing blank items, and as you may remember, it turned out that we can enforce that very easily in the frontend. Avoiding duplicate items, however, is less straightforward to do in the frontend (although not impossible, of course), so this chapter will lean more heavily on server-side validation, and bubbling errors from the backend back up to the UI.
This chapter goes into the more intricate details of Django’s forms framework, so you have my official permission to skim through it if you already know all about customising Django forms and how to display errors in the UI, or if you’re reading this book for the TDD rather than for the Django.
If you’re still learning Django, there’s good stuff in here! If you want to just skim-read, that’s OK too. Make sure you take a quick look at “An Aside on When to Test for Developer Silliness”, and “Recap: What to Test in Views” at the end.
Another FT for Duplicate Items
We add a second test method to ItemValidationTest,
and tell a little story about what we want to see happen
when a user tries to enter the same item twice into their to-do list:
src/functional_tests/test_list_item_validation.py (ch16l001)
deftest_cannot_add_duplicate_items(self):# Edith goes to the home page and starts ...
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