26.3.2 Template Short-Circuiting

Before we make any changes to our code, it’s worth noting that we have already made a few optimizations to our website. The least obvious of the bunch is the order of condition checks in templates.

The Django Template Language (DTL) is rife with clever optimizations. One of the most powerful is condition short-circuiting. In Example 26.2, for instance, consider the condition in startup_detail.html.

Example 26.2: Project Code

organizer/templates/organizer/startup_detail.html in eb0dc1e1d1

114   {% if perms.blog.view_future_post and startup.blog_   .         posts.all %}

The order of conditions in the DTL matter. In the preceding code, Django first checks our privileges, which are ...

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.