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.
Common errors
-
djlint: command not found
cause The `djlint` executable is not in your system's PATH, usually because it wasn't installed or the Python environment where it was installed is not active.fixInstall `djlint` using pip: `pip install djlint`. If it's already installed, ensure your Python environment is activated or that Python's script directory is in your system's PATH. -
H030: Consider adding a meta description.
cause This is a linting suggestion from `djlint`'s HTML best practices rules (specifically for SEO), not an error preventing execution. Similar warnings like 'H031: Consider adding meta keywords.' also appear.fixYou can either add the suggested `<meta>` tags to your HTML or ignore the rules via the command line (`djlint your_file.html --ignore H030,H031`) or in a configuration file (`ignore = ["H030", "H031"]` in `djlint.toml`). -
djLint is not installed for the current active Python interpreter.
cause This error typically occurs when using the `djlint` VS Code extension, indicating that `djlint` is not found in the currently selected Python interpreter's environment within VS Code.fixSelect the correct Python interpreter in VS Code that has `djlint` installed, or install `djlint` into the active virtual environment using `pip install -U djlint`. Alternatively, configure the VS Code extension settings (`djlint.useVenv` and `djlint.pythonPath`) to point to a globally installed `djlint` or specific Python executable.
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")