我正在编写一个Django应用程序,我使用VSCode (settings.json
)中的以下配置来自动格式化我的Python代码(我也使用Django VSCode扩展):
{
"liveshare.authenticationProvider": "GitHub",
"editor.fontSize": 16,
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"html.format.endWithNewline": true,
"files.exclude": {
"**/__pycache__": true
},
"explorer.confirmDragAndDrop": false,
"editor.formatOnSave": true,
"git.confirmSync": false,
"window.zoomLevel": -1,
"python.linting.flake8Enabled": true,
"python.formatting.provider": "black",
"python.linting.flake8Args": [
"--ignore=E501,E266,W503"
],
"files.associations": {
"**/*.html": "html",
"**/templates/**/*.html": "django-html",
"**/templates/**/*": "django-txt",
"**/requirements{/**,*}.{txt,in}": "pip-requirements",
"*.html": "django-html"
},
"emmet.includeLanguages": {"django-html": "html"},
}
虽然Python文件中的格式按预期工作,但它似乎也干扰了我的Django模板并破坏了它们。
例如,以下模板..。
{% extends "base.html" %}
{% load martortags %}
{% block title %}MyBlog - {{ object.title }}{% endblock title %}
{% block content %}
<ol>
{% for post in object_list %}
<li>
<a href="{{ post.get_absolute_url }}">{{ post.title }}</a>
</li>
{% endfor %}
</ol>
{% endblock content %}
保存后的...becomes:
{% extends "base.html" %} {% load martortags %} {% block title %}MyBlog - {{
object.title }}{% endblock title %} {% block content %}
<ol>
{% for post in object_list %}
<li><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></li>
{% endfor %}
</ol>
{% endblock content %}
正如在settings.json
中所看到的,我试图遵循Django VSCode扩展文档中的说明,但是它没有工作。事实上,无论"files.associations"
和"emmet.includeLanguages"
设置是否存在于settings.json
中,都不会发生任何变化。
如何将.py
文件格式(由VSCode正确识别为Python
文件)与.html
文件格式(由VSCode正确识别为Django Template
文件)分离,并可能对后者使用普通的HTML?
发布于 2022-01-05 07:48:29
Python的black
格式化程序不会格式化.html
文件。
我不确定您为.html
文件选择了哪种格式。您可以查看一下User settings.json
,尝试找到以下内容:
"[html]": {
"editor.defaultFormatter": xxx
}
您可以右键单击编辑器并选择Format Document with
以找出导致它的格式。Prettier Code formatter
扩展看起来可能导致它。
https://stackoverflow.com/questions/70570285
复制相似问题