5👍
you can disable the default html formatter, goto File > Preferences > User or Workspace Settings, in HTML settings you will find :
// Enable/disable default HTML formatter (requires restart)
"html.format.enable": true,
I think VSCode uses js-beautify as default formatter, you can use beautify extension to override it settings with .jsbeautifyrc in project directory
26👍
I used the beautify extension instead which worked right away while prettier was still putting on one line. Credit goes to kimanihuon on this stackoverflow page.
- Add the following to settings.json
"files.associations": { "**/*.html": "html", "**/templates/*/*.html": "django-html", "**/templates/*": "django-txt", "**/requirements{/**,*}.{txt,in}": "pip-requirements" }, "emmet.includeLanguages": { "django-html": "html" },
- Install beautify and then add following to settings.json
"beautify.language": { "html": [ "htm", "html", "django-html" ] },
- Restart VSCode just in case
- [Django]-How to output Django queryset as JSON?
- [Django]-Error: Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as <script>
- [Django]-How can I get the file name from request.FILES?
21👍
Alexa has a good point in her answer. The file mode needs to be changed in “Django/HTML” to prevent VS CODE for formatting it.
How to change the file mode?
A quick solution is to use this extension called vscode-Django and adjust your setting like this as said in his documentation.
"files.associations": {
"**/templates/*.html": "django-html",
"**/templates/*": "django-txt"
}
With those setting any file located in the template folder will be considered as a Django template file and will not be affected by HTML autoformat.
PS: The extension is still in preview mode, hope it will get better with time.
- [Django]-How can I check the size of a collection within a Django template?
- [Django]-How do I include related model fields using Django Rest Framework?
- [Django]-Negating a boolean in Django template
17👍
Changing the language mode of the file to “Django/HTML” will also prevent VSCode from autoformatting it.
- [Django]-Conversion of datetime Field to string in django queryset.values_list()
- [Django]-Pypi see older versions of package
- [Django]-What is the best location to put templates in django project?
7👍
Prettier in VSCode was the culprit in my situation. I got it to stop formatting with following in my settings.json
.
"files.associations": {
"**/*.html": "html",
"**/templates/*/*.html": "django-html",
"**/templates/*": "django-txt",
"**/requirements{/**,*}.{txt,in}": "pip-requirements"
},
"[django-html]": {
"editor.defaultFormatter": "batisteo.vscode-django"
},
files.associations
sets the language mode for the templates to django-html.
[django-html]
sets the settings for that language mode. As of writing this, the formatter in batisteo.vscode-django
doesn’t do anything for me (so it’s the same as null
in its place), but I left it there in case the django extension ever does.
- [Django]-What does "'tests' module incorrectly imported" mean?
- [Django]-Django query datetime for objects older than 5 hours
- [Django]-Django: 400 bad request syntax – what does this message mean?
4👍
Had the same issue, found a post where the person disabled JS-CSS-HTML Formatter extension (https://stackoverflow.com/a/42100808/4812548) and it fixed the issue. Tested on mine and it seems to have worked too. Hope that helps
- [Django]-How to get Gunicorn to use Python 3 instead of Python 2 (502 Bad Gateway)
- [Django]-Circular dependency in Django Rest Framework serializers
- [Django]-How to pass a variable from settings.py to a view?
4👍
I tried and now highly recommend the djLint extension to format Django-HTML code. It is effective and quite configurable.
You need to install the Python extension (works with venv) and the VSCode extension.
With your code, the output is:
{% for row in 'ABCDEFGH' %}
<tr>
{% for col in '123456789012345' %}
<td style="text-align: center; border: 1px solid #aaa;">
{% with forloop.counter|stringformat:"s" as counter %}
{% with row|add:counter as seat_num %}
{% if seat_num not in oc_seats %}
<input type="checkbox" value="{{ row }}{{ forloop.counter }}" name="seats">
{% endif %}
{{ seat_num }}
{% endwith %}
{% endwith %}
</td>
{% endfor %}
</tr>
{% endfor %}
- [Django]-How to check the TEMPLATE_DEBUG flag in a django template?
- [Django]-Getting 'str' object has no attribute 'get' in Django
- [Django]-How do I render jinja2 output to a file in Python instead of a Browser
3👍
You can disable the formatting option for some of the languages:
Go to Extensions and then "Extension settings" of Prettier and add django-html for this case as below.
- [Django]-Is there a filter for divide for Django Template?
- [Django]-Django Admin – Specific user (admin) content
- [Django]-Sort order of Django Admin records
1👍
vscode eslint does not work on Django files. so to disable the prettier extension just add this to your settings.json
"[django-html]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
A similar solution would be
"[django-html]": {
"editor.formatOnSave": false
},
worked for me.
- [Django]-What is a Django "app" supposed to mean?
- [Django]-Django – Website Home Page
- [Django]-How to get GET request values in Django?
0👍
in VSCode’s settings.json, add the following : emmet.includeLanguages": {"django-html": "html"}
- [Django]-Aggregating save()s in Django?
- [Django]-How can I trigger a 500 error in Django?
- [Django]-Is it possible to use FastAPI with Django?
0👍
I found Better Jinja extension very useful this allows correct formatting of django-template and emmet abbreviations for HTML.
- Open settings by pressing Ctrl+Shift+P and search "Open User Settings (JSON)"
- Add following code in JSON file
"material-icon-theme.languages.associations": {
"jinja-html": "html"
},
"files.associations": {
"*.html": "jinja-html"
},
"emmet.includeLanguages": {
"jinja-html": "html"
},
- Make sure you’ve installed Better Jinja extension.
- [Django]-Running django tutorial tests fail – No module named polls.tests
- [Django]-Django get a QuerySet from array of id's in specific order
- [Django]-Set Django IntegerField by choices=… name