Skip to main content

DjHTML

A pure-Python Django template indenter without dependencies.

DjHTML is a Django template indenter that works with mixed HTML/CSS/Javascript templates. It works similar to other code-formatting tools such as Black. The goal is to correctly indent already well-structured templates but not to fix broken ones.

For example, DjHTML converts the following badly indented template:

<!doctype html>
<html>
    <body>
        {% block content %}
        Hello, world!
        {% endblock %}
        <script>
            $(function() {
            console.log('Hi mom!');
            });
        </script>
    </body>
</html>

To the following:

<!doctype html>
<html>
    <body>
        {% block content %}
            Hello, world!
        {% endblock %}
        <script>
            $(function() {
                console.log('Hi mom!');
            });
        </script>
    </body>
</html>

Installation

You can install DjHTML with the following command:

$ pip install djhtml

Usage

After installation you can indent Django templates using the djhtml command. The default is to write the indented output to standard out. To modify the source file in-place, use the -i / --in-place option:

$ djhtml -i template.html
Successfully reformatted template.html

The other available options are:

  • -h / --help: show overview of available options
  • -q / --quiet: don't print any output
  • -t / --tabwidth: set tabwidth (default is 4)
  • -o / --output-file: write output to specified file

Pre-commit configuration

You can use DjHTML as a pre-commit hook to automatically indent your Django templates upon each commit.

First, install pre-commit:

$ pip install pre-commit
$ pre-commit install

Then, add the following to your .pre-commit-config.yaml:

repos:
- repo: https://github.com/rtts/djhtml
  rev: main
  hooks:
  - id: djhtml

Finally, run the following command:

$ pre-commit autoupdate

Now when you run git commit you will see something like the following output:

$ git commit

djhtml...................................................................Failed
- hook id: djhtml
- files were modified by this hook

Successfully reformatted template.html

To inspect the changes that were made, use git diff. If you are happy with the changes, you can commit them normally. If you are not happy, please do the following:

  1. Run SKIP=djhtml git commit to commit anyway, skipping the djhtml hook.

  2. Consider opening an issue with relevant part of the input file that was incorrectly formatted, and an example of how it should have been formatted.

Your feedback for improving DjHTML is very welcome!

Metadata

Release files for djhtml 1.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for djhtml 1.2.2
File Size Uploaded
djhtml-1.2.2.tar.gz 21.1 kB Details

Release files / djhtml-1.2.2.tar.gz

Download URL djhtml-1.2.2.tar.gz
Size 21.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7de2c8ed264d599a9c0f7c015ece850d275b90ffca21ccce0b017d08efb5c99e
BLAKE2b-256 checksum
How to use checksums
e7de173661142ef302d1e56de3e2a9243544d2bcfa0618d5513db0c6e7368638
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

Release history Release notifications | RSS feed

3.0.11

2 release files

3.0.9

2 release files

3.0.8

1 release file

3.0.7

1 release file

3.0.6

1 release file

3.0.5

1 release file

3.0.4

1 release file

3.0.3

1 release file

3.0.2

1 release file

3.0.1

1 release file

3.0.0

1 release file

2.0.0

1 release file

1.5.2

1 release file

1.5.1

1 release file

1.5.0

1 release file

1.4.14

1 release file

1.4.13

1 release file

1.4.12

1 release file

1.4.11

1 release file

1.4.10

1 release file

1.4.9

1 release file

1.4.8

1 release file

1.4.7

1 release file

1.4.6

1 release file

1.4.5

1 release file

1.4.4

1 release file

1.4.3

1 release file

1.4.2

1 release file

1.4.1

1 release file

1.4.0

1 release file

1.3.0

1 release file

This release

1.2.2 This release

1 release file

1.2.1

1 release file

1.2.0

1 release file

1.0.2

1 release file

1.0.1

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page