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/badge/Coverage-100%25-success?style=for-the-badge https://img.shields.io/pypi/v/djade.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit
Any color you like, as long as it’s jade.

A Django template formatter.

Based on Django’s template style guide. Very fast because it’s built in Rust: benchmarked taking 20ms to format 377 templates.


Improve your Django and Git skills with my books.


Installation

Use pip:

python -m pip install djade

Python 3.8 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.1"]  # 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 on any file in a directory 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.1 templates/eggs/*.html
Rewriting templates/eggs/dodo.html
Rewriting templates/eggs/ostrich.html

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.1 templates/eggs/*.html
Rewriting templates/eggs/quail.html

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 -0 djade

…or PowerShell’s ForEach-Object:

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

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.

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:

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

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

    -{% load omelette frittata %}
    +{% load friattata omelette %}
  • Inside variables, no spaces around filters:

    -{{ egg | crack }}
    +{{ egg|crack }}
  • Inside tags, single spaces between tokens:

    -{% if  breakfast  ==  'scrambled eggs'  %}
    +{% if breakfast == 'scrambled eggs' %}
  • Unindent top-level {% block %} and {% endblock %} tags when {% extends %} is used:

    -  {% extends 'egg.html' %}
    +{% extends 'egg.html' %}
    
    -  {% block yolk %}
    +{% block yolk %}
         ...
    -  {% endblock yolk %}
    +{% endblock yolk %}

Extra rules:

  • No leading empty lines:

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

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

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

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

    -{% load omelette %}
    -
    -{% load frittata %}
    +{% load frittata omelette %}
  • Unindent {% extends %} tags:

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

 {% extends 'egg.html' %}

-
 {% block yolk %}
   ...
 {% endblock yolk %}
+
 {% block white %}
   ...
 {% endblock white %}

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 eggs|length_is:1 %}
+{% if eggs|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="":

-{% egg_data|json_script:"" %}
+{% egg_data|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:

-{% trans "Egg types" %}
+{% translate "Egg types" %}

-{% blocktrans with colour=egg.colour %}
+{% blocktranslate with colour=egg.colour %}
 This egg 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 egg.colour 'golden' %}
+{% if egg.colour == 'golden' %}
 golden
-{% endifequal %}
+{% endif %}

-{% ifnotequal egg.colour 'silver' %}
+{% if egg.colour != 'silver' %}
 not silver
-{% 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 %}

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.0.0.tar.gz (31.2 kB view details)

Uploaded Source

Built Distributions

djade-1.0.0-py3-none-win_arm64.whl (824.2 kB view details)

Uploaded Python 3 Windows ARM64

djade-1.0.0-py3-none-win_amd64.whl (900.7 kB view details)

Uploaded Python 3 Windows x86-64

djade-1.0.0-py3-none-win32.whl (816.3 kB view details)

Uploaded Python 3 Windows x86

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

Uploaded Python 3 musllinux: musl 1.1+ x86-64

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

Uploaded Python 3 musllinux: musl 1.1+ ARM64

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

Uploaded Python 3 manylinux: glibc 2.17+ x86-64

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

Uploaded Python 3 manylinux: glibc 2.17+ i686

djade-1.0.0-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.1 MB view details)

Uploaded Python 3 manylinux: glibc 2.17+ ARMv7l

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

Uploaded Python 3 manylinux: glibc 2.17+ ARM64

djade-1.0.0-py3-none-macosx_11_0_arm64.whl (1.0 MB view details)

Uploaded Python 3 macOS 11.0+ ARM64

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

Uploaded Python 3 macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: djade-1.0.0.tar.gz
  • Upload date:
  • Size: 31.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for djade-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4f3786244dd5bd5cf31f9bc61763f8bbf4b0329b8d1ccddd4e14efb175ed7b97
MD5 92a480652b2e6ee435c7cca73244d937
BLAKE2b-256 38b6d4934df1dbbe746943299b66953837542f13175e8553b31072a1d6fab68b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: djade-1.0.0-py3-none-win_arm64.whl
  • Upload date:
  • Size: 824.2 kB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for djade-1.0.0-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 d92d5d4d30c96c429634c0f0306a37d17ca7f4f134ae62570902e786d745e05a
MD5 b118e9ce9c8787db33aabdafba2dd75d
BLAKE2b-256 a232d6c8b5b57e8d9fee322170d395397421e27b6b0b0e9f13d044e84bd571de

See more details on using hashes here.

File details

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

File metadata

  • Download URL: djade-1.0.0-py3-none-win_amd64.whl
  • Upload date:
  • Size: 900.7 kB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for djade-1.0.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 9f625d8b84534ea39ad63a3fb0c3ddf4b61ed24f61a45a904838f10a8b4775f4
MD5 7ce393de2fecf0ee1e1c937905f72444
BLAKE2b-256 9aa35ee66d21262516db2c8459f226f1f3c6d19a73008a54d0eb00fc6bd1ce57

See more details on using hashes here.

File details

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

File metadata

  • Download URL: djade-1.0.0-py3-none-win32.whl
  • Upload date:
  • Size: 816.3 kB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for djade-1.0.0-py3-none-win32.whl
Algorithm Hash digest
SHA256 51a335c227a7f23ae5cf3443af09327a35370ed604f10ddab09d7256cf82180b
MD5 21db3b476248dfe5015b2ee90f01e1b7
BLAKE2b-256 3bd0f5d2428bb44388a544ce4bed6d9d6d0822bb0aa1c0aec48267bdd53050d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for djade-1.0.0-py3-none-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 8177c9ecfd8eff026b91d5c5772655d65a3e7cf039472d2f81fb67ac1315f4f6
MD5 2ad1d71bcf87a7f45d8af0e0125807d9
BLAKE2b-256 412f6b2af220bf2d1ad60e6e4bf62ff8d404d0e183bed867edf21bc942037c8e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for djade-1.0.0-py3-none-musllinux_1_1_aarch64.whl
Algorithm Hash digest
SHA256 2f2d78f4e44a89c6d0778e2deebf17f4058e881cc405171a15ec738a9bc176c2
MD5 24696d2fdd29b281e9a4a64e534630e4
BLAKE2b-256 f13715d400b451680955d3083345d186cb7e32c8ab8be6c87ed6e6d3f3590a56

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for djade-1.0.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d938ec96e9dbb412deb2525449b88a2e59fcf436ed3bfd4e1726e1b301193285
MD5 bfd4400d537f51f386d7b3d8b1eab015
BLAKE2b-256 30e5f2119fa265f5e75e4da64d016371fa122d706f59400aed8d15f73a1df185

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for djade-1.0.0-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 a508a822e23e9efdc888ced794694990fad4686f79092d07e13af1653d05fb84
MD5 40b66dcfbe98246f1f4059eeeb8eba3c
BLAKE2b-256 49960416957248f591a7424ab7a15cfeb202af040cc874953c0a5887d2d1a785

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for djade-1.0.0-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 3fcae66c51941f19a9d5fe7ed93dcef9f3af171a29922cc286f8d8051ba5ad19
MD5 353cfa607db73e93d3a93608e94db135
BLAKE2b-256 acd372a98ae0cf69de05c85e8db7451c3433eab0ccf9f7cb256625f13cdbe399

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for djade-1.0.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 26370f5b872a78ed82d4c616af63d9d271cdb1fcf9cecfdb3fa3b878a1add1d0
MD5 04188c60fd11f6de4e9c7e7426e47e94
BLAKE2b-256 3416cff3b8dd8504c5eebfca17a676c1be0c4f02668e8e9a6af186dce6354bf6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for djade-1.0.0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1d52cd98fbbdd44014f596ef2f7220c1c6268422f44eda5b15652119670cc2b5
MD5 a2b417eeca066da13acbfc0ea2d9e6b4
BLAKE2b-256 66705596c938bc976bda9870a6be9fd7ac6d200266069c9fe1517da87e3e7a35

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for djade-1.0.0-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b0f060d3555fbcfa5f1a76c8d3edfff930c9d3ad8c0c22f5f73d20470e7a4a3b
MD5 bc13e13e08d38bd5012f0fc262636554
BLAKE2b-256 8063947ac9dac77dfa562250b964f7029a3a4afa3372ac3a913c43bebdfec640

See more details on using hashes here.

Supported by

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