8.2.4 Maintaining the DRY Principle in tag_form.html

Our understanding of displaying forms in templates has focused entirely on minute control of the form. However, we have done so at the cost of the DRY principle. In the following subsection, we significantly shorten the code used to display forms—at the cost of control of our HTML.

8.2.4.1 Replacing Loops and Conditions with Variables

One of the key shortcuts Django provides is the ability to call variables directly to provide looping behavior. For example, we are currently looping over form.non_field_errors (form errors not specific to any field), form.name.errors, and form.slug.errors. The latter is displayed in Example 8.12.

Example 8.12: Project Code

organizer/templates/organizer/tag_form.html ...

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.