Linter Usage

djLint includes many rules to check the style and validity of your templates. Take full advantage of the linter by configuring it to use a preset profile for the template language of your choice.

djlint /path/to/templates --lint

# with custom extensions
djlint /path/to/templates -e html.dj --profile=django

# or to file
djlint /path/to/this.html.j2 --profile=jinja

Custom Rules

Create a file .djlint_rules.yaml alongside your pyproject.toml. Rules can be added to this files and djLint will pick them up.

A good rule follows this pattern:

- rule:
name: T001
message: Find Trichotillomania
flags: re.DOTALL|re.I
patterns:
- Trichotillomania

Code Patterns

The first letter of a code follows the pattern:

  • D: applies specifically to Django
  • H: applies to html
  • J: applies specifically to Jinja
  • M: applies specifically to Handlebars
  • N: applies specifically to Nunjucks
  • T: applies generally to templates

Rules

CodeMeaning
D004(Django) Static urls should follow {% static path/to/file %} pattern.
D018(Django) Internal links should use the {% url ... %} pattern.
H005Html tag should have lang attribute.
H006img tag should have height and width attributes.
H007<!DOCTYPE ... > should be present before the html tag.
H008Attributes should be double quoted.
H009Tag names should be lowercase.
H010Attribute names should be lowercase.
H011Attribute values should be quoted.
H012There should be no spaces around attribute =.
H013img tag should have alt attributes.
H014More than 2 blank lines.
H015Follow h tags with a line break.
H016Missing title tag in html.
H017Tag should be self closing.
H019Replace javascript:abc() with on_ event and real url.
H020Empty tag pair found. Consider removing.
H021Inline styles should be avoided.
H022Use HTTPS for external links.
H023Do not use entity references.
H024Omit type on scripts and styles.
H025Tag seems to be an orphan.
H026Empty id and class tags can be removed.
H029Consider using lowercase form method values.
H030Consider adding a meta description.
H031Consider adding meta keywords.
H033Extra whitespace found in form action.
J004(Jinja) Static urls should follow {{ url_for('static'..) }} pattern.
J018(Jinja) Internal links should use the {% url ... %} pattern.
T001Variables should be wrapped in a single whitespace. Ex: {{ this }}
T002Double quotes should be used in tags. Ex {% extends "this.html" %}
T003Endblock should have name. Ex: {% endblock body %}.
T027Unclosed string found in template syntax.
T028Consider using spaceless tags inside attribute values. {%- if/for -%}
T032Extra whitespace found in template tags.
T034Did you intend to use {% … %} instead of {% … }%?

Adding Rules

We welcome pull requests with new rules!

A good rule consists of

  • Name
  • Code
  • Message - Message to display when error is found.
  • Flags - Regex flags. Defaults to re.DOTALL. ex: re.I|re.M
  • Patterns - regex expressions that will find the error.
  • Exclude - Optional list of profiles to exclude rule from.

Please include a test to validate the rule.

Edit this page Updated Sep 16, 2022