djLint

1.36.4 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to use `djlint` from the command line within a Python script. It creates a sample HTML file, then runs `djlint` to lint and reformat it. The primary interaction with `djlint` is via its CLI.

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")

view raw JSON →