O'Reilly logo

Django Unleashed by Andrew Pinkham

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required