VSCode breaks Django template tags with newline
Asked Answered
S

7

8

Problem:

{% extends 'base.html' %} {% block title %} Dashboard {% endblock %} {% block pagetitle %}

becomes

{% extends 'base.html' %} {% block title %} Dashboard {% endblock %} {% block
pagetitle %}

Note that the {% tag %} is being broken with a new line. This causes syntax errors with django templates.

I've tried most top django template extensions and this does not fix the issue.

I've also tried these settings:

    "[html]": {
        "editor.formatOnSave": false,
    },
    "html.format.wrapLineLength": 0,
    "html.format.enable": false,
    "prettier.disableLanguages": ["html"] 

Desired Behavior:

  1. Automatically format *.html files, while preserving django template tags, not breaking them up with newlines.
  2. Sub-optimal (but acceptable) behavior: don't format *.html files at all.
Sur answered 11/11, 2020 at 11:49 Comment(1)
For those who says Django is popular, VSCode doesn't agree ))Gumma
A
4

Open settings.json file and add this line to it. it worked for me.

"[django-html]": {
   "editor.formatOnSave": false
},
Amling answered 9/5, 2023 at 9:3 Comment(1)
I already have it, doesn't helpGumma
N
1

I had the same issue and the only way I found that solved it is to disable the default HTML formatter. Unfortunately, I did not find a way to make it format Django template tags correctly. You can do the same if you go to VS Code Preferences > Settings > User > Extensions > HTML and uncheck 'Enable/disable default HTML formatter'.

enter image description here

Nostrum answered 16/1, 2021 at 12:32 Comment(0)
U
1

I solved this by following this advice: https://mcmap.net/q/713811/-prettier-vscode-extension-not-support-django-template-tags-tag

TLDR: install the djLint extension (and remember to do $ pip install djlint)

Unreeve answered 31/10, 2022 at 20:40 Comment(0)
H
1

VSCode extensions + Settings update

To format automatically Django templates in VS Code, I use three steps.

First step: install dependencies

Second step: detect Django templates

You need to change file associations in VS Code settings.

Open VS Code JSON settings file (Press F1, then type "Open User Settings (JSON)".

Add in this JSON file:

    "files.associations": {
        "**/templates/**/*.html": "django-html",
        "**/*.html": "html",
    },

Third step: auto-format Django templates

In the same JSON file, activate auto-formatting by adding

    "[django-html]": {
        "editor.defaultFormatter": "monosans.djlint",
        "editor.formatOnSave": true,
        "editor.tabSize": 2
    },

Optional: customize djlint config

If necessary, you can configure the formatter, by reading the documentation here: https://www.djlint.com/docs/configuration/

Hagiography answered 23/9, 2023 at 17:8 Comment(0)
T
0

I got it to work by simply adding {{""}} between the {% tag %} that were being broken.

Example:

{% extends 'main/base.html' %} {% block title_block %}Homepage{% endblock%}
{{""}} {%block style_ref_block%}{%endblock%} {{""}} {% block body_block %}
Traitorous answered 11/2, 2021 at 11:27 Comment(0)
T
0

This Didn't work for me.

The hack I found was to set the vscode language to jinja instead of the auto detected html

enter image description here

Triquetrous answered 9/5, 2022 at 18:54 Comment(0)
W
0

I've also just experienced vs-code misbehaving on django template tags (i.e. deleting curly braces).

I don't like the idea of disabling HTML formatting just to support templates (i.e. vs-code Preferences/Settings/Extensions/HTML: disable (uncheck) "HTML>Format:Enable"). This is arguably a step backwards, but it does stop vs-code misbehaving.

Instead, I chose to install (vs-code Preferences/Extensions) the 'Django' extension, by Baptiste Darthenay. This was a better way to go, because it works, gracefully, preserves native vs-code HTML formatting, and includes a nice set of django snippits, which saves me keystrokes when embedding template code. Tada!

BTW, before finding Baptiste's awesome extension, I also tried keeping vs-code HTML formatting enabled, AND enabling 'HTML>Format:Templating', which promised to "Honor django and other templating language tags"; it did not.

Wrigley answered 19/6, 2022 at 0:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.