8.2.5 Looping over Form Fields

The worst error we are making here is probably the most subtle to beginners. We are manually declaring each and every field in our form: we reference name and slug fields directly. This is fine on a small form like TagForm, but on a larger form, it becomes unwieldy. What’s more, any changes to TagForm’s fields will result in changes in our template, which we never, ever want. We can entirely avoid this unwieldy result by creating a form loop to iterate through all of the fields in our form, entirely replacing our code with that shown in Example 8.21.

Example 8.21: Project Code

organizer/templates/organizer/tag_form.html in f48adbb1a2

13     {% for field in form %} 14       {{ field.errors ...

Get Django Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.