# Automatic formatting of Django templates

**URL:** <https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341>\
**Category:** Getting Started\
**Created:** [October 14, 2019, 1:42am UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341 "2019-10-14T01:42:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![leigh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leigh/32/3623_2.png) [@leigh](https://forum.djangoproject.com/u/leigh)\
**Post date:** [October 14, 2019, 1:42am UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/1 "2019-10-14T01:42:43Z")

</div>

Does anyone here autoformat their Django template files? If so, do you have suggestions for an autoformatting tool to use?

I wish I could use Prettier, but they have a ticket for supporting Django (and j2 and similar) templates sitting open and waiting for someone to implement it ☹

---

<div class="post-metadata">

**Author:** ![andrewgodwin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/andrewgodwin/32/2_2.png) [@andrewgodwin](https://forum.djangoproject.com/u/andrewgodwin)\
**Post date:** [October 15, 2019, 6:11am UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/2 "2019-10-15T06:11:45Z")

</div>

I sadly do not, I think the rise of auto-formatters is recent enough it has sort of passed them by. Would love to see it, though.

---

<div class="post-metadata">

**Author:** ![leigh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leigh/32/3623_2.png) [@leigh](https://forum.djangoproject.com/u/leigh)\
**Post date:** [October 15, 2019, 11:58pm UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/3 "2019-10-15T23:58:38Z")

</div>

I feel like the challenge is going to be building a parser that understands the semantics of Django’s template language and HTML at the same time, given the myriad weird ways the two can be interspersed.

I have some ideas, but lack an in-depth knowledge of how the existing tools in the space work. I really want to dive in and learn and experiment, but the last thing I need right now is another side project, no matter how much I want to 😆

---

<div class="post-metadata">

**Author:** ![rixx](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rixx/32/29_2.png) [@rixx](https://forum.djangoproject.com/u/rixx)\
**Post date:** [October 17, 2019, 10:12pm UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/4 "2019-10-17T22:12:09Z")

</div>

I have looked into this repeatedly in the past, because I’m frustrated with my inconsistent HTML files. All existing solutions out there do not seem to support Jinja/Django style markup in any meaningful way, and mangle template files a s a result. I’d be very happy to see any solution, but yeah – it’s not going to be easy.

---

<div class="post-metadata">

**Author:** ![EmilStenstrom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/emilstenstrom/32/363_2.png) [@EmilStenstrom](https://forum.djangoproject.com/u/EmilStenstrom)\
**Post date:** [October 18, 2019, 6:30pm UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/5 "2019-10-18T18:30:26Z")

</div>

Hi! I wonder if you could use code from a code syntax file? There’s a pretty good sublime text package that can handle Django templates.

---

<div class="post-metadata">

**Author:** ![leigh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leigh/32/3623_2.png) [@leigh](https://forum.djangoproject.com/u/leigh)\
**Post date:** [November 1, 2019, 5:41am UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/6 "2019-11-01T05:41:59Z")

</div>

I’m not sure if that would offer auto formatting, at least not to the extent of something like Black or Prettier; I think text editor syntax files only really do lexing and some basic indentation rules. Unless the relevant ST package offers formatting as a built-in feature?

I had a go at building something the other week, and decided after an hour or so that it required more expertise and time that I was able to give it ☹

---

<div class="post-metadata">

**Author:** ![rtrtrtrtrtrt](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@rtrtrtrtrtrt](https://forum.djangoproject.com/u/rtrtrtrtrtrt)\
**Post date:** [March 25, 2020, 1:23am UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/7 "2020-03-25T01:23:59Z")

</div>

Has anyone managed to find anything suitable for VS Code? Would love to use autoformatting for Django templates.

---

<div class="post-metadata">

**Author:** ![ddahan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ddahan/32/3370_2.png) [@ddahan](https://forum.djangoproject.com/u/ddahan)\
**Post date:** [February 3, 2021, 11:17am UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/8 "2021-02-03T11:17:39Z")

</div>

I switched from PyCharm to VSCode (and think it truly worths it), but the only thing I’m still struggling with is this Django template formatting.

An ugly workaround is to use [PyCharm command line formatting tool](https://www.jetbrains.com/help/pycharm/command-line-formatter.html).  
But each formatting action takes 5 seconds, and of course it’s not a standalone tool, you need PyCharm installed locally. I can’t name this a “solution”.

@andrewgodwin I’m questioning myself: would an official VS Code Django plugin be something impossible to develop internally in Django teams?

My thoughts about this:

- VS Code is becoming a “no-brainer choice” code editing tool for many people. As a personal opinion, I see no point using PyCharm today except for a Django project. But dealing with two IDEs is painful.

- Even if one disagree about the statement above, I don’t see a Django beginner spending money to buy PyCharm pro Edition (the free edition has no real Django support), especially because VS Code is free. Which means the first experience this beginner will get with Django templates will be … not perfect.

- Many developers love Django, but they are and more and more used to a top-level tooling experience, especially in JS community. Auto-formatters are getting really mainstream with tools like Prettier. I have the feeling that many developers could decide NOT using Django templates, just because front-end development experience is way better with tools like Vue.js, almost out of the box.

- Some modern frameworks come with their own official plugin (I’m thinking of [TailwindCSS Intellisense](https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss)) and it’s amazing. It just works. To me, it’s truly more valuable when it’s official. It brings trust, and removes the need of choosing among 10 different plugins that overlaps themselves.

I know it takes huge work, but I’m curious to hear your point of view about the prioritization of such work, compared to classic features.

---

<div class="post-metadata">

**Author:** ![andrewgodwin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/andrewgodwin/32/2_2.png) [@andrewgodwin](https://forum.djangoproject.com/u/andrewgodwin)\
**Post date:** [February 3, 2021, 4:49pm UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/9 "2021-02-03T16:49:35Z")

</div>

I think having a proper plugin would be lovely, but it’s not something the current core team has the time or (for most of us) the skillset to develop; a template auto-formatter, in particular, would have to be authored somewhat from scratch.

This is the sort of thing, though, that could be developed outside of the Django core code and then officially recommended or that sort of thing. I do think it’s worth it, I just don’t think it’s something we can just… do.

---

<div class="post-metadata">

**Author:** ![elsayedg](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/elsayedg/32/3582_2.png) [@elsayedg](https://forum.djangoproject.com/u/elsayedg)\
**Post date:** [February 20, 2021, 11:59am UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/10 "2021-02-20T11:59:24Z")

</div>

The only solution I found this video on YouTube.

[![](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/e/ee8895cfcdc34741822c8ea4f99eb0aa576b093b.jpeg "Django Template configuration with HTML on VS Code") ](https://www.youtube.com/watch?v=j8twQx0G0Cg)

“he does not speak English” but it would not matter it all about install 3 extensions, and it will autocomplete Django template and HTML too.

---

<div class="post-metadata">

**Author:** ![leigh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leigh/32/3623_2.png) [@leigh](https://forum.djangoproject.com/u/leigh)\
**Post date:** [February 23, 2021, 9:21pm UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/11 "2021-02-23T21:21:02Z")

</div>

I’m not sure if I’m missing something here—as best I can tell, that video’s only about autocompletion and snippets, rather than auto formatting ☹

---

<div class="post-metadata">

**Author:** ![elsayedg](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/elsayedg/32/3582_2.png) [@elsayedg](https://forum.djangoproject.com/u/elsayedg)\
**Post date:** [February 24, 2021, 9:09am UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/12 "2021-02-24T09:09:26Z")

</div>

What I did, I use Perrier to formal HTML then I switch to Django and write all my code if the code need more format I switch back to HTML, that is the best thing till now for me

---

<div class="post-metadata">

**Author:** ![SandAndTrees](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sandandtrees/32/4381_2.png) [@SandAndTrees](https://forum.djangoproject.com/u/SandAndTrees)\
**Post date:** [April 29, 2021, 7:51pm UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/13 "2021-04-29T19:51:06Z")

</div>

[prettydiff](https://prettydiff.com/documentation.xhtml) does a good job. hope it helps anyone, also good for other template languages like twig etc.

`prettydiff beautify source:<your file>`

---

<div class="post-metadata">

**Author:** ![christianmlong](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christianmlong/32/4741_2.png) [@christianmlong](https://forum.djangoproject.com/u/christianmlong)\
**Post date:** [June 3, 2021, 8:08pm UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/14 "2021-06-03T20:08:38Z")

</div>

[djhtml](https://github.com/rtts/djhtml) looks promising for cleaning up the formatting of Django template code.

---

<div class="post-metadata">

**Author:** ![christianmlong](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christianmlong/32/4741_2.png) [@christianmlong](https://forum.djangoproject.com/u/christianmlong)\
**Post date:** [June 3, 2021, 8:42pm UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/15 "2021-06-03T20:42:02Z")

</div>

Update, djhtml cleans up indentation, but not other kinds of formatting.

---

<div class="post-metadata">

**Author:** ![christopherpickering](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christopherpickering/32/5327_2.png) [@christopherpickering](https://forum.djangoproject.com/u/christopherpickering)\
**Post date:** [July 30, 2021, 2:24pm UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/16 "2021-07-30T14:24:29Z")

</div>

I had the same pain and couldn’t find anything working well. There was an extension for sublimetext, but it only worked sometimes… So I put together an experimental one called [djLint](https://github.com/Riverside-Healthcare/djlint), which lints templates and can also format complete html template docs.  
Assuming you use a custom file extension of “html.dj”, you can run

`djlint /path --extension=html.dj --check`

to see what it thinks about your files.

---

<div class="post-metadata">

**Author:** ![jeff](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jeff/32/55_2.png) [@jeff](https://forum.djangoproject.com/u/jeff)\
**Post date:** [August 27, 2021, 11:22pm UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/17 "2021-08-27T23:22:48Z")

</div>

I’m here to +1 [djhtml](https://github.com/rtts/djhtml). I have been using it for a few months and it does enough safely, that it’s been auto-formatting all of my projects ever since.

If indentation is what you are looking for then it’s worth checking out.

---

<div class="post-metadata">

**Author:** ![wavo89](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/wavo89/32/7920_2.png) [@wavo89](https://forum.djangoproject.com/u/wavo89)\
**Post date:** [March 26, 2022, 4:31pm UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/19 "2022-03-26T16:31:21Z")

</div>

Thing is an autocomplete takes care of tags, so one is not very likely to have a bad indentation, rather they don’t want to have to indent manually in the first place as you are auto dropping the tags in. The flow of tag completion + save → autoformat (even if just indentation) is great. djhtml will only fix bad indentation, not format to a prescribed indentation for you.

---

<div class="post-metadata">

**Author:** ![ddahan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ddahan/32/3370_2.png) [@ddahan](https://forum.djangoproject.com/u/ddahan)\
**Post date:** [May 4, 2022, 1:58pm UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/20 "2022-05-04T13:58:25Z")

</div>

Here is what’s working best for me currently within VS Code:

- I installed djhtml, as advised.
- To run it on file saving, I installed [runOnSave](https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave) extension and configured it to target Django HTML files only:

```auto
"emeraldwalk.runonsave": {
    "commands": [
        {
            "match": ".*/templates/.*\\.html$", // only in templates dir
            "cmd": "djhtml -i ${file} -t 2"
        },
    ]
},

```

Used in conjunction with [this extension](https://marketplace.visualstudio.com/items?itemName=batisteo.vscode-django) (syntax color and snippets), this makes using django templates ok imo (not perfect, but acceptable).

I guess a future improvment of this workflow could be to build a VS Code formatter extension that embbed djhtml, rather than installing a python package. Any volonteer ? 🙂

---

<div class="post-metadata">

**Author:** ![ddahan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ddahan/32/3370_2.png) [@ddahan](https://forum.djangoproject.com/u/ddahan)\
**Post date:** [June 29, 2022, 10:14am UTC](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341/21 "2022-06-29T10:14:12Z")

</div>

For now it seems these 3 tools are used and appreciated by the community:

- djhtml
- [djlint](https://github.com/Riverside-Healthcare/djlint)
- [curlylint](https://github.com/thibaudcolas/curlylint)

Is anyone able to provide quick comparison between them? Otherwise I’ll test all of them by myself.  
Thanks.

[Next page](https://forum.djangoproject.com/t/automatic-formatting-of-django-templates/341.md?page=2)
