# Django template style guide extensions

**URL:** <https://forum.djangoproject.com/t/django-template-style-guide-extensions/35351>\
**Category:** Django Internals\
**Created:** [October 3, 2024, 10:42am UTC](https://forum.djangoproject.com/t/django-template-style-guide-extensions/35351 "2024-10-03T10:42:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [October 3, 2024, 10:42am UTC](https://forum.djangoproject.com/t/django-template-style-guide-extensions/35351/1 "2024-10-03T10:42:12Z")

</div>

In [this thread](https://forum.djangoproject.com/t/django-template-style-guide-ticket-14831/28177), we implemented a style guide in Django’s contribution guide, thanks to @rcheley .

I recently created a formatter tool, [Djade](https://github.com/adamchainz/djade), that follows that guide plus a bit more. Based on this experience, I propose the following changes to the template style guide.

1. One `{% load %}` tag per line, alphabetically sorted. @nessita pinged on the previous thread about this, and [the djade issue](https://github.com/adamchainz/djade/issues/33) has broad support.

2. Prefer `{% load ... from ... %}` syntax over `{% load ... %}. This is equivalent to PEP 8's recommendation to prefer `from … import …`over`from … import \*`. @ericholscher reminded me of this syntax when [reporting an issue in Djade](https://github.com/adamchainz/djade/issues/62). It’s clearer, safer, and I can see why Read the Docs prefers it.

3. In templates using `{% extends %}`, one line between top-level tags, except between comments and the following tag, and `{% load %}` tags:

4. Single spaces at the start and end of comments: `{# comment #}`. The style guide currently describes this style for tags and variables - comments were just missed.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [October 3, 2024, 10:55am UTC](https://forum.djangoproject.com/t/django-template-style-guide-extensions/35351/2 "2024-10-03T10:55:47Z")

</div>

I’m not a fan of 1 and 2. Both of these feel like overreach TBH

No problem with 3 and 4 though.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 3, 2024, 11:03am UTC](https://forum.djangoproject.com/t/django-template-style-guide-extensions/35351/3 "2024-10-03T11:03:32Z")

</div>

Definitely not a fan of #2. (Thinking of all the `{% load static %}` and `{% load crispy_forms_tags %}` directives across all my templates.)

---

<div class="post-metadata">

**Author:** ![nessita](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nessita/32/12194_2.png) [@nessita](https://forum.djangoproject.com/u/nessita)\
**Post date:** [October 3, 2024, 12:27pm UTC](https://forum.djangoproject.com/t/django-template-style-guide-extensions/35351/4 "2024-10-03T12:27:46Z")

</div>

I’m a fan of #1, opposite of a fan for #2, and +1 for #3 and #4.

For #2, I just learned today that the `load ... from ...` syntax is possible! However, I don’t agree with Eric’s comparison to the Python import syntax. I think it’s a bit of a stretch, as Python modules can contain many items that might be shadowed by local definitions, which can lead to namespace pollution and complicate/pollute imports for others.

In the case of Django templates, I don’t believe this concern applies in the same way. Additionally, changing every single `load` directive in thousands of projects seems more tedious than beneficial (I have never seen a project using `load ... from ...` so far!). Because of this, I’m -1 to #2.

---

<div class="post-metadata">

**Author:** ![matthiask](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matthiask/32/115_2.png) [@matthiask](https://forum.djangoproject.com/u/matthiask)\
**Post date:** [October 3, 2024, 1:26pm UTC](https://forum.djangoproject.com/t/django-template-style-guide-extensions/35351/5 "2024-10-03T13:26:51Z")

</div>

I very seldom have merge conflicts in the `{% load %}` line in templates, so I slightly prefer the load tag to contain all used template tag libraries, but either choice wouldn’t cause me to remove djade from my projects. That being said, I like alphabetic sorting a lot.

I basically never use `{% load ... from ... %}` and I fully agree with @nessita 's reasoning there. The only time something like this was necessary I instead wrote my own template library registering the offending tag under a different name.

Definitely agree with 3. and 4.

---

<div class="post-metadata">

**Author:** ![apollo13](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/apollo13/32/30_2.png) [@apollo13](https://forum.djangoproject.com/u/apollo13)\
**Post date:** [October 3, 2024, 1:47pm UTC](https://forum.djangoproject.com/t/django-template-style-guide-extensions/35351/6 "2024-10-03T13:47:50Z")

</div>

I agree with Carlton.

I also roughly never had conflicts bit adding the new querystring tag might do so for some users.

---

<div class="post-metadata">

**Author:** ![DmytroLitvinov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dmytrolitvinov/32/6228_2.png) [@DmytroLitvinov](https://forum.djangoproject.com/u/DmytroLitvinov)\
**Post date:** [October 3, 2024, 5:44pm UTC](https://forum.djangoproject.com/t/django-template-style-guide-extensions/35351/7 "2024-10-03T17:44:50Z")

</div>

I am a big fun of #1 and use it heavily on all production sites.  
I prefer that way because of search where that specific template tag is used and refactor it in case I rename module to another name and I search thought problem like `{% load {MODULENMAE} %}`  
Also, for my it is visually better to distinguish which template tags I load in that template.

---

<div class="post-metadata">

**Author:** ![tom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tom/32/6818_2.png) [@tom](https://forum.djangoproject.com/u/tom)\
**Post date:** [November 4, 2024, 1:44pm UTC](https://forum.djangoproject.com/t/django-template-style-guide-extensions/35351/8 "2024-11-04T13:44:45Z")

</div>

For 2, I will add the extra argument that often I struggle to figure out if a loaded template library is actually used at all without looking through several files. Having `{% load ... from ... %}` would help me know when I can remove the load entirely if I’ve stopped using the tags in there. I don’t always remember where every tag and filter comes from.
