Is there a code formatter for Django templates?

Viewed 282

Where can I find a code formatter that reformats this template:

{% block content %}
{{ data|json_script:"data" }}
<div id="app">
    <app></app>
</div>

{% render_bundle 'chunk-vendors' %}
{% render_bundle 'vue-dashboard' %}

{% endblock %

to this format (notice the indentation):

{% block content %}
    {{ data|json_script:"data" }}
    <div id="app">
        <app></app>
    </div>

    {% render_bundle 'chunk-vendors' %}
    {% render_bundle 'vue-dashboard' %}

{% endblock %}

I've used PyCharm to do it, but I'm looking for a standalone tool.

1 Answers

I had the same question and couldn't find anything so started a tool called djLint, with can

  • lint for common html/template issues
  • check template formatting (read:indentation)
  • reformat a template

Theres another one out there called djhtml as well, but it requires that you already have all the line breaks you need in your code.. while djLint can take a single line of sloppy html and add the needed line breaks.

Neither is a parser, so if you put in bad code, you will get bad code out :)

Related