Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a code formatter for Django templates?

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.

like image 596
RobertPro Avatar asked Sep 17 '25 20:09

RobertPro


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

like image 193
sur.la.route Avatar answered Sep 19 '25 10:09

sur.la.route