Skip to Content
Test-Driven Development with Python, 3rd Edition
book

Test-Driven Development with Python, 3rd Edition

by Harry Percival
October 2025
Intermediate to advanced
712 pages
14h 24m
English
O'Reilly Media, Inc.
Content preview from Test-Driven Development with Python, 3rd Edition

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)

def test_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

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Effective Python: 125 Specific Ways to Write Better Python, 3rd Edition

Effective Python: 125 Specific Ways to Write Better Python, 3rd Edition

Brett Slatkin

Publisher Resources

ISBN: 9781098148706Errata Page