Skip to main content

A Django template formatter.

Project description

https://img.shields.io/github/actions/workflow/status/adamchainz/djade/main.yml.svg?branch=main&style=for-the-badge https://img.shields.io/pypi/v/djade.svg?style=for-the-badge pre-commit
You can have any colour you like, as long as it’s jade.

A Django template formatter.

You can have any colour you like, as long as it’s [d]jade.

Djade formats Django template syntax with a style based on the template style guide in Django’s documentation. It does not format HTML or other templated languages.

Djade is fast because it is built in Rust: benchmarked taking 20ms to format 377 templates.

Read more in the introductory post, or below.


Improve your Django and Git skills with my books.


Installation

Use pip:

python -m pip install djade

Python 3.9 to 3.13 supported.

pre-commit hook

You can also install Djade as a pre-commit hook.

First, add the following to the repos section of your .pre-commit-config.yaml file (docs):

-   repo: https://github.com/adamchainz/djade-pre-commit
    rev: ""  # Replace with the latest tag on GitHub
    hooks:
    -   id: djade
        args: [--target-version, "5.2"]  # Replace with Django version

The separate repository enables installation without compiling the Rust code.

The default configuration uses pre-commit’s files option to pick up all text files in directories called templates (source). You may wish to override this if you have templates in different directories by adding files to the hook configuration in your .pre-commit-config.yaml file.

Second, format your entire project:

pre-commit run djade --all-files

Check these changes for any potential Djade bugs and commit them. Try git diff --ignore-all-space to check non-whitespace changes.

Third, consider adding the previous commit SHA to a .git-blame-ignore-revs file. This will prevent the initial formatting commit from showing up in git blame.

Keep the hook installed to continue formatting your templates. pre-commit’s autoupdate command will upgrade Djade so you can take advantage of future features.

Usage

djade is a command line tool that rewrites files in place. Pass a list of template files to format them:

$ djade --target-version 5.2 templates/engine.html
1 file reformatted

Djade can also upgrade some old template syntax. Add the --target-version option with your Django version as <major>.<minor> to enable applicable fixers:

$ djade --target-version 5.2 templates/engine.html
1 file reformatted

Djade does not have any ability to recurse through directories. Use the pre-commit integration, globbing, or another technique to apply it to many files. For example, with git ls-files | xargs:

git ls-files -z -- '*.html' | xargs -0r djade

…or PowerShell’s ForEach-Object:

git ls-files -- '*.html' | %{djade $_}

The filename - makes Djade read from standard input and write to standard output. In this case, Djade always exits with code 0, even if changes were made.

Options

--target-version

Optional: the version of Django to target, in the format <major>.<minor>. If provided, Djade enables its fixers for versions up to and including the target version. See the list of available versions with djade --help.

--check

Avoid writing any formatted files back. Instead, exit with a non-zero status code if any files would have been modified, and zero otherwise.

Formatting

Djade aims to format Django template syntax in a consistent, clean way. It wants to be like Black: opinionated and free of configuration. Djade’s style is based on the rules listed in the Django contribution style guide’s template style section, plus some more.

Djade does not aim to format the host language of templates (HTML, etc.). That is a much broader scope and hard to do without semantic changes. For example, whitespace is significant in some HTML contexts, such as in <pre> tags, so even adjusting indentation can affect the meaning.

Below are the rules that Djade implements.

Rules from the Django style guide:

  • Single spaces at the start and end of variables and tags:

    -{{train}}
    +{{ train }}
    
    -{%  blow whistle  %}
    +{% blow whistle %}
  • Label {% endblock %} tags that aren’t on the same line as their opening {% block %} tag:

     {% block funnel %}
     ...
    -{% endblock %}
    +{% endblock funnel %}
  • Sort libraries in {% load %} tags:

    -{% load coal boiler %}
    +{% load boiler coal %}
  • Inside variables, no spaces around filters:

    -{{ fire | stoke }}
    +{{ fire|stoke }}
  • Inside tags, single spaces between tokens:

    -{% if  locomotive  ==  'steam engine'  %}
    +{% if locomotive == 'steam engine' %}
  • Unindent top-level {% block %} and {% endblock %} tags when {% extends %} is used:

    -  {% extends 'engine.html' %}
    +{% extends 'engine.html' %}
    
    -  {% block boiler %}
    +{% block boiler %}
       ...
    -  {% endblock boiler %}
    +{% endblock boiler %}

Extra rules:

  • No leading empty lines:

    -
     {% extends 'engine.html' %}
     ...
  • No trailing empty lines:

     ...
     {% endblock wheels %}
    -
    -
  • Single spaces at the start and end of comments:

    -{#choo choo#}
    +{# choo choo #}
  • No labels in {% endblock %} tags on the same line as their opening {% block %} tag:

    -{% block funnel %}...{% endblock funnel %}
    +{% block funnel %}...{% endblock %}
  • Merge consecutive {% load %} tags:

    -{% load boiler %}
    -
    -{% load coal %}
    +{% load boiler coal %}
  • Sort loaded items in {% load ... from .. %} tags:

-{% load steam heat from boiler %}
+{% load heat steam from boiler %}
  • Unindent {% extends %} tags:

    -  {% extends 'engine.html' %}
    +{% extends 'engine.html' %}
  • Exactly one blank line between top-level {% block %} and {% endblock %} tags when {% extends %} is used:

 {% extends 'engine.html' %}

-
 {% block funnel %}
   ...
 {% endblock funnel %}
+
 {% block boiler %}
   ...
 {% endblock boiler %}

Fixers

Djade applies the below fixes based on the target Django version from --target-version.

Django 4.2+: length_is -> length

From the release note:

The length_is template filter is deprecated in favor of length and the == operator within an {% if %} tag.

Djade updates usage of the deprecated filter within if tags, without other conditions, appropriately:

-{% if engines|length_is:1 %}
+{% if engines|length == 1 %}

Django 4.1+: empty ID json_script fixer

From the release note:

The HTML <script> element id attribute is no longer required when wrapping the json_script template filter.

Djade removes the argument where json_script is passed an empty string, to avoid emitting id="":

-{% tracks|json_script:"" %}
+{% tracks|json_script %}

Django 3.1+: trans -> translate, blocktrans / endblocktrans -> blocktranslate / endblocktranslate

From the release note:

The renamed translate and blocktranslate template tags are introduced for internationalization in template code. The older trans and blocktrans template tags aliases continue to work, and will be retained for the foreseeable future.

Djade updates the deprecated tags appropriately:

-{% load blocktrans trans from i18n %}
+{% load blocktranslate translate from i18n %}

-{% trans "Engine colours" %}
+{% translate "Engine colours" %}

-{% blocktrans with colour=engine.colour %}
+{% blocktranslate with colour=engine.colour %}
 This engine is {{ colour }}.
-{% endblocktrans %}
+{% endblocktranslate %}

Django 3.1+: ifequal and ifnotequal -> if

From the release note:

The {% ifequal %} and {% ifnotequal %} template tags are deprecated in favor of {% if %}.

Djade updates the deprecated tags appropriately:

-{% ifequal engine.colour 'blue' %}
+{% if engine.colour == 'blue' %}
 Thomas!
-{% endifequal %}
+{% endif %}

-{% ifnotequal engine.colour 'blue' %}
+{% if engine.colour != 'blue' %}
 Not Thomas.
-{% endifnotequal %}
+{% endif %}

Django 2.1+: admin_static and staticfiles -> static

From the release note:

{% load staticfiles %} and {% load admin_static %} are deprecated in favor of {% load static %}, which works the same.

Djade updates {% load %} tags appropriately:

-{% load staticfiles %}
+{% load static %}

-{% load admin_static %}
+{% load static %}

Django 1.3+: legacy variable assignment syntax

The minimum target Django version is 2.1, so this fixer is always active.

Django 1.3 added support for = to assign variables in {% with %} and {% blocktranslate %} tags. Prior to this, they only supported the legacy syntax using the as keyword, which Django still supports.

Djade rewrites the older as syntax to the newer = one:

-{% with engines.count as total %}
+{% with total=engines.count %}
     ...
 {% endwith %}

-{% blocktranslate with engine.colour as colour %}
+{% blocktranslate with colour=engine.colour %}
     ...
 {% endblocktranslate %}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djade-1.5.0.tar.gz (40.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

djade-1.5.0-py3-none-win_arm64.whl (987.3 kB view details)

Uploaded Python 3Windows ARM64

djade-1.5.0-py3-none-win_amd64.whl (1.1 MB view details)

Uploaded Python 3Windows x86-64

djade-1.5.0-py3-none-win32.whl (963.3 kB view details)

Uploaded Python 3Windows x86

djade-1.5.0-py3-none-musllinux_1_1_x86_64.whl (1.2 MB view details)

Uploaded Python 3musllinux: musl 1.1+ x86-64

djade-1.5.0-py3-none-musllinux_1_1_aarch64.whl (1.1 MB view details)

Uploaded Python 3musllinux: musl 1.1+ ARM64

djade-1.5.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

djade-1.5.0-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl (1.2 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ i686

djade-1.5.0-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.0 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARMv7l

djade-1.5.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.1 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

djade-1.5.0-py3-none-macosx_11_0_arm64.whl (1.1 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

djade-1.5.0-py3-none-macosx_10_12_x86_64.whl (1.1 MB view details)

Uploaded Python 3macOS 10.12+ x86-64

File details

Details for the file djade-1.5.0.tar.gz.

File metadata

  • Download URL: djade-1.5.0.tar.gz
  • Upload date:
  • Size: 40.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for djade-1.5.0.tar.gz
Algorithm Hash digest
SHA256 fee13ad8142338df9a658fc3789113bd08a518e56261cfe301a964faf62fc0cc
MD5 b51b4ceb99861f32a96a6a865f0ae476
BLAKE2b-256 f18bd39ad663b5f647f1f93f363c6a90817a77f89605bf3516043923c15eda88

See more details on using hashes here.

Provenance

The following attestation bundles were made for djade-1.5.0.tar.gz:

Publisher: main.yml on adamchainz/djade

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file djade-1.5.0-py3-none-win_arm64.whl.

File metadata

  • Download URL: djade-1.5.0-py3-none-win_arm64.whl
  • Upload date:
  • Size: 987.3 kB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for djade-1.5.0-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 dfd431b77170e26458e59a1ed886d588adc6e25f8e3a4ae1d76af230cee4b8e7
MD5 c3fa3753381fc1a977a4375bf7ccb23f
BLAKE2b-256 854e1621968558fd67d044bf915bb491310cc65111daa71404b70c0b352c4791

See more details on using hashes here.

Provenance

The following attestation bundles were made for djade-1.5.0-py3-none-win_arm64.whl:

Publisher: main.yml on adamchainz/djade

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file djade-1.5.0-py3-none-win_amd64.whl.

File metadata

  • Download URL: djade-1.5.0-py3-none-win_amd64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for djade-1.5.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 67892f9405d92decd8bb4a253b14534df6a1f613a2423a197317d530599b8dbe
MD5 c9466480bda8a2fdd7f58bade61ec7bc
BLAKE2b-256 b9b7b0de0ec2b63da159a7c3916f0b2781f15273a66c0d2e21ae2804dd9e9e62

See more details on using hashes here.

Provenance

The following attestation bundles were made for djade-1.5.0-py3-none-win_amd64.whl:

Publisher: main.yml on adamchainz/djade

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file djade-1.5.0-py3-none-win32.whl.

File metadata

  • Download URL: djade-1.5.0-py3-none-win32.whl
  • Upload date:
  • Size: 963.3 kB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for djade-1.5.0-py3-none-win32.whl
Algorithm Hash digest
SHA256 8045469ebe36d5503622c7268d8f7859a1f0eb579725a363ec18b508fdf53363
MD5 d10e09afca271d76d23ed7ac493c7f56
BLAKE2b-256 d20c3b94d02d90497ae33e9a07336acec2d472898e739460788c4227fc70b5a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for djade-1.5.0-py3-none-win32.whl:

Publisher: main.yml on adamchainz/djade

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file djade-1.5.0-py3-none-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for djade-1.5.0-py3-none-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 4eeb82c65b7baad7fb27c139f00d68e9ac35114d5cc169e45964b571e54c4c66
MD5 be91bad38cb46f49c395b17a53ba6bfb
BLAKE2b-256 288bac7ae206bddbff197ae9fc7c7f6e6168aa25e193724677af2d740692188b

See more details on using hashes here.

Provenance

The following attestation bundles were made for djade-1.5.0-py3-none-musllinux_1_1_x86_64.whl:

Publisher: main.yml on adamchainz/djade

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file djade-1.5.0-py3-none-musllinux_1_1_aarch64.whl.

File metadata

File hashes

Hashes for djade-1.5.0-py3-none-musllinux_1_1_aarch64.whl
Algorithm Hash digest
SHA256 a1f4ecfc68aedbaeb8d9c5b6079abf6fbcafbf59e420d9f9d11da0ed3906c675
MD5 055d56b871bf511db5150a86e15dd4ce
BLAKE2b-256 490fd2bb803ef67929b9eb69364a2700712497efebe0d7477f75842e956acecf

See more details on using hashes here.

Provenance

The following attestation bundles were made for djade-1.5.0-py3-none-musllinux_1_1_aarch64.whl:

Publisher: main.yml on adamchainz/djade

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file djade-1.5.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for djade-1.5.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b586e3b60b23c173531e20ad7c7a964e2d10f39ee57fe2ddbba2a6342508d71e
MD5 969f9ea0db72dfb2f81bd11c14289351
BLAKE2b-256 9bb20b28716ad2cee69f0204b74b3603d815aa482f2bdd3a543e00581af27134

See more details on using hashes here.

Provenance

The following attestation bundles were made for djade-1.5.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: main.yml on adamchainz/djade

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file djade-1.5.0-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for djade-1.5.0-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 6e8aa4650ed2b67f2ac9eaaeb7fd6f8915e9a4a0ba48648255d19db0df35ba2e
MD5 bfdd65dc8e13697e770946e390834f78
BLAKE2b-256 3f6edfc27c64231564e7a9ce18e7380eef08e9ce6a1ea00c222b078617345fc4

See more details on using hashes here.

Provenance

The following attestation bundles were made for djade-1.5.0-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl:

Publisher: main.yml on adamchainz/djade

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file djade-1.5.0-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for djade-1.5.0-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 e6f50ed54c4867fe64525b17465ced86a5364cc69f4d976bc31f3b6a41971375
MD5 22421ea6fb6d01648a6f29af033d38af
BLAKE2b-256 13ba381d195ad63a601ff08e19cda6e9b53396b2ac3e6a930258a13bf834b48e

See more details on using hashes here.

Provenance

The following attestation bundles were made for djade-1.5.0-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl:

Publisher: main.yml on adamchainz/djade

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file djade-1.5.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for djade-1.5.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 17160411fd96be8dd7f44b9bd140bfd62cd045a12c5a52e0a44700f50a416895
MD5 f53adfb2204061a23bd110a1ac22dcdb
BLAKE2b-256 98ebad535e0155257a7bc9cbf5cc62568f2005116f95234e44cae370565f31c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for djade-1.5.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: main.yml on adamchainz/djade

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file djade-1.5.0-py3-none-macosx_11_0_arm64.whl.

File metadata

  • Download URL: djade-1.5.0-py3-none-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 3, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for djade-1.5.0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eeccde54dfc4e465663c3bdd387a3f3a69213cc03d1a1c91b9f155bd35e62523
MD5 35a3f801040268ddc2ef57022123eab7
BLAKE2b-256 541512b65b515fa1e584d1b9acab588a9fde10eef7fe02540f856eaefb03cf9e

See more details on using hashes here.

Provenance

The following attestation bundles were made for djade-1.5.0-py3-none-macosx_11_0_arm64.whl:

Publisher: main.yml on adamchainz/djade

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file djade-1.5.0-py3-none-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for djade-1.5.0-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 16a6cbc275b223ba7acada78b6f607232534f24ae3926c82e0e10b7c37e8d961
MD5 adf9f2af4cf10d27cb1988cc8d0c1bb1
BLAKE2b-256 c407e99c555b00f9a4816539eda3f67d6cbe12e6f338627040455eb1bf60f061

See more details on using hashes here.

Provenance

The following attestation bundles were made for djade-1.5.0-py3-none-macosx_10_12_x86_64.whl:

Publisher: main.yml on adamchainz/djade

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page