djLint
djLint is an HTML template linter and formatter that helps ensure consistency and identify common issues across various template languages, including Django, Jinja, Nunjucks, Handlebars, GoLang, and Angular. It is primarily a command-line interface (CLI) tool. As of version 1.36.4, it continues to receive regular updates with a focus on performance improvements and bug fixes.
Warnings
- breaking Python 3.8 support was dropped in version 1.35.3. Users on Python 3.8 or older will need to upgrade their Python environment to use newer versions of djLint.
- gotcha Reformatting may duplicate file contents or incorrectly handle non-ASCII characters on Windows with specific older versions.
- gotcha Configuration files (e.g., `pyproject.toml`, `.djlintrc`) might be ignored if placed in the project root on some older Python versions.
- gotcha Reformatting does not work reliably with long JSON/HTML content embedded directly into attribute data within templates.
- gotcha When `format_attribute_template_tags` is enabled (for Jinja/Nunjucks), conditional attributes without spaceless tags (e.g., `{% if condition %}` instead of `{% if condition -%}`) can lead to unwanted whitespace after formatting, potentially breaking rendering or styling.
Install
-
pip install djlint
Imports
- Config
from djlint.settings import Config
- get_line
from djlint.lint import get_line
Quickstart
import subprocess
import os
# Create a dummy HTML file for demonstration
with open("test_template.html", "w") as f:
f.write("""
<div class="my-class" >
<h1> Hello, {{ name }}! </h1>
</div>
""")
print("--- Original File ---")
with open("test_template.html", "r") as f:
print(f.read())
# Run djlint to check for linting issues
print("\n--- Linting Check ---")
lint_result = subprocess.run(["djlint", "test_template.html", "--lint"], capture_output=True, text=True)
print(lint_result.stdout)
if lint_result.stderr:
print(lint_result.stderr)
# Run djlint to reformat the file
print("\n--- Reformatting File ---")
reformat_result = subprocess.run(["djlint", "test_template.html", "--reformat"], capture_output=True, text=True)
print(reformat_result.stdout) # Should be empty if reformat is applied directly to file
if reformat_result.stderr:
print(reformat_result.stderr)
print("\n--- Formatted File ---")
with open("test_template.html", "r") as f:
print(f.read())
# Clean up the dummy file
os.remove("test_template.html")