### Output Example Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/getting-started.md The resulting output after running a reformat command. ```html
``` -------------------------------- ### Install djLint Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/getting-started.md Install the djLint package using common package managers. ```bash pip install djlint ``` ```bash uv tool install djlint ``` ```bash pipx install djlint ``` ```bash brew install djlint ``` ```bash npm i djlint ``` -------------------------------- ### Setup local development environment Source: https://github.com/djlint/djlint/blob/master/README.md Commands to synchronize the project dependencies and execute the test suite using uv. ```bash # install uv first: https://docs.astral.sh/uv/getting-started/installation/ uv sync # run the test suite uv run pytest ``` -------------------------------- ### HTML Template Formatting Examples Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/formatter.md Comparison of raw, standard formatted, and single-attribute-per-line formatted HTML templates. ```html {% load admin_list %} {% load i18n %}{% if pagination_required %} {% for i in page_range %} {% paginator_number cl i %} {% endfor %} {% endif %} {{ cl.result_count }} {% if cl.result_count == 1 %} {{ cl.opts.verbose_name }} {% else %} {{ cl.opts.verbose_name_plural }} {% endif %} {% if show_all_url %} {% translate 'Show all' %} {% endif %} {% if cl.formset and cl.result_count %} {% endif %}
``` ```html {% load admin_list %} {% load i18n %}{% if pagination_required %} {% for i in page_range %} {% paginator_number cl i %} {% endfor %} {% endif %} {{ cl.result_count }} {% if cl.result_count == 1 %} {{ cl.opts.verbose_name }} {% else %} {{ cl.opts.verbose_name_plural }} {% endif %} {% if show_all_url %} {% translate 'Show all' %} {% endif %} {% if cl.formset and cl.result_count %} {% endif %}
``` ```html {% load admin_list %} {% load i18n %}{% if pagination_required %} {% for i in page_range %} {% paginator_number cl i %} {% endfor %} {% endif %} {{ cl.result_count }} {% if cl.result_count == 1 %} {{ cl.opts.verbose_name }} {% else %} {{ cl.opts.verbose_name_plural }} {% endif %} {% if show_all_url %} {% translate 'Show all' %} {% endif %} {% if cl.formset and cl.result_count %} {% endif %}
``` -------------------------------- ### Python Rule Implementation Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/linter.md Example of a Python module implementing a run() function for custom linting logic. ```python from typing import Any, Dict, List from djlint.settings import Config from djlint.lint import get_line import re def run( rule: Dict[str, Any], config: Config, html: str, filepath: str, line_ends: List[Dict[str, int]], *args: Any, **kwargs: Any, ) -> List[Dict[str, str]]: """ Rule that fails if if the html file contains 'bad'. This is just an example, in reality it's much simpler to do that with "pattern rule". """ errors: List[Dict[str, str]] = [] for match in re.finditer(r"bad", html): errors.append({ "code": rule["name"], "line": get_line(match.start(), line_ends), "match": match.group().strip()[:20], "message": rule["message"], }) return errors ``` -------------------------------- ### Custom Pattern Rule Configuration Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/linter.md Example of defining a custom rule in .djlint_rules.yaml that triggers on a regex pattern match. ```yaml - rule: name: T001 message: Find Trichotillomania flags: re.DOTALL|re.I patterns: - Trichotillomania ``` -------------------------------- ### Reformat Django Template with djLint Source: https://github.com/djlint/djlint/blob/master/README.md Example of a raw Django template before and after applying the djLint reformat command with attribute splitting. ```django {% block content %}No posts yet.
{%endif%}No posts yet.
{% endif %}Shipping is free.
Delivery takes 3 days.
Shipping is free.
Delivery takes 3 days.
``` -------------------------------- ### H006: Require height and width on img tags Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/linter.md Define image dimensions to prevent layout shifts during page loading. Enable this rule with --include=H006. ```html
```
```html
```
--------------------------------
### H010: Use lowercase attribute names
Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/linter.md
Lowercase attribute names ensure compatibility and consistent text searching across templates.
```html
```
```html
```
--------------------------------
### Enforce self-closing void tags (H017)
Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/linter.md
Use XHTML-style self-closing slashes for void elements. Enable with --include=H017.
```html
two
``` ```htmltwo
``` -------------------------------- ### Enforce plain void tag endings (H018) Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/linter.md Use plain > endings for void elements to follow the HTML living standard. Enable with --include=H018. ```html{{ user.name }
``` ```django{{ user.name }}
``` -------------------------------- ### Not Recommended Spaces Around Conditional Attributes Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/best-practices.md This pattern is not recommended as djLint removes whitespace inside conditional statements, potentially leading to unexpected formatting. ```html{# djlint:on #}
{% comment %} djlint:off H025 {% endcomment %}
{% comment %} djlint:on {% endcomment %} {{!-- djlint:off H025 --}}
{{!-- djlint:on --}} {{ /* djlint:off H025 */ }}
{{ /* djlint:on */ }} ``` -------------------------------- ### Recommended Spaces Around Conditional Attributes Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/best-practices.md djLint removes whitespace inside conditional statements. This pattern is recommended for cleaner formatting. ```html
```
```html
```
--------------------------------
### Recommended Spaceless Conditional Attributes
Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/best-practices.md
When `format_attribute_template_tags` is enabled, use spaceless tags within conditional attributes to prevent unwanted spaces. This pattern is recommended.
```html
```
--------------------------------
### Enforce self-closing meta tags (H035)
Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/linter.md
Use this rule to ensure meta tags are XHTML-compatible by including a trailing slash.
```html
```
```html
```
--------------------------------
### Use literal characters instead of entity references (H023)
Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/linter.md
Use literal UTF-8 characters instead of entity references for better readability and to avoid rendering errors.
```html
Dates 1900 — 2000
``` ```htmlDates 1900 — 2000
``` -------------------------------- ### Validate matching block tags (T038) Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/linter.md Ensure all template block tags have corresponding closing tags to prevent template syntax errors. ```django {% if user.is_authenticated %}Welcome back!
``` ```django {% if user.is_authenticated %}Welcome back!
{% endif %} ``` -------------------------------- ### H008: Use double quotes for attributes Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/linter.md Standardize on double quotes for attribute values to improve consistency and avoid issues with apostrophes. ```html ``` ```html ``` -------------------------------- ### Add line breaks after heading tags (H015) Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/linter.md Ensure block-level elements follow headings to maintain document structure and clean diffs. ```htmlIntro text.
``` ```htmlIntro text.
``` -------------------------------- ### Remove empty id and class attributes (H026) Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/linter.md Remove empty id or class attributes as they serve no purpose and can indicate template bugs. ```htmlSaved.
``` ```htmlSaved.
``` -------------------------------- ### T002: Double Quotes in Tags Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/linter.md Standardizes the use of double quotes within template tags to ensure consistency across the codebase. ```html {% extends 'base.html' %} ``` ```html {% extends "base.html" %} ``` -------------------------------- ### T003: Named Endblock Tags Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/linter.md Requires endblock tags to include the block name to prevent nesting errors and improve readability. ```html {% block content %}hello
{% endblock %} ``` ```html {% block content %}hello
{% endblock content %} ``` -------------------------------- ### Require title tag in HTML (H016) Source: https://github.com/djlint/djlint/blob/master/docs/src/docs/linter.md Every complete HTML document must include a title element for accessibility and SEO. ```html Content ``` ```htmlHello
``` ```htmlHello