django模板上的vscode html自动格式化

django模板上的vscode html自动格式化,html,django,django-templates,visual-studio-code,Html,Django,Django Templates,Visual Studio Code,我喜欢在save autoformat上使用VSCode,直到它把我的模板代码弄糟 它错误地将django模板语法格式化为一行代码(有时是很长的一行)。因此,与其使用此代码 {% for row in 'ABCDEFGH' %} <tr> {% for col in '123456789012345' %} <td> {% with forloop.counter|stringformat:"s" as counter %} {%

我喜欢在save autoformat上使用VSCode,直到它把我的模板代码弄糟

它错误地将django模板语法格式化为一行代码(有时是很长的一行)。因此,与其使用此代码

{% for row in 'ABCDEFGH' %}
<tr>
  {% for col in '123456789012345' %}
    <td>
      {% 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 %}
          <br> {{ seat_num }} 
        {% endwith %}
      {% endwith %}
     </td>    
   {% endfor %}
</tr>
{% endfor %}
{% 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 %}
{%ABCDEFGH%%中的行
“'123456789012345%”中列的{%
{%与forloop.counter | stringformat:“s”作为计数器%}
{行中的百分比|添加:计数器作为座位_num%}
{%if seat_num不在oc_seats%}
{%endif%}

{{seat_num}} {%endwith%} {%endwith%} {%endfor%} {%endfor%}
我最终得到了这个密码

{% for row in 'ABCDEFGH' %}
<tr>
  {% for col in '123456789012345' %}
    <td>
      {% 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 %}
          <br> {{ seat_num }} 
        {% endwith %}
      {% endwith %}
     </td>    
   {% endfor %}
</tr>
{% endfor %}
{% 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 %}
{%ABCDEFGH%%中的行
“'123456789012345%”中列的{%
{%with forloop.counter | stringformat:“s”作为计数器%}{%with row | add:counter作为seat_num%}{%if seat_num不在oc_seats%}
{%endif%}{{seat_num}{%endwith%}{%endwith%}{%endwith%}
{%endfor%}
{%endfor%}
我试图通过将用户设置更改为
{“editor.formatOnSave”:false}
来禁用保存时的格式,但仍然没有得到任何运气

有什么配置可以让它更好地工作吗

附言:
我在Sierra MacOSx上使用的VSCode 1.9版也有同样的问题,发现一篇帖子,其中此人禁用了JS-CSS-HTML格式化程序扩展()并修复了该问题。在我的车上测试过,似乎也很有效。希望对您有所帮助

您可以在html设置中禁用默认的html格式化程序“转到文件>首选项>用户或工作区设置”,您将发现:

// Enable/disable default HTML formatter (requires restart)
  "html.format.enable": true,

我认为VSCode使用js beautify作为默认格式化程序,您可以使用项目目录中的.jsbeautifyrc覆盖它的设置,将文件的语言模式更改为“Django/HTML”也会阻止VSCode自动格式化它。

Alexa在这方面有很好的优势。需要在“Django/HTML”中更改文件模式,以防止VS代码对其进行格式化

如何更改文件模式

一个快速的解决方案是使用vscode Django并像他在文档中所说的那样调整您的设置

"files.associations": {
    "**/templates/*.html": "django-html",
    "**/templates/*": "django-txt"
}
通过这些设置,位于template文件夹中的任何文件都将被视为Django模板文件,并且不会受到HTML autoformat的影响


PS:扩展仍处于预览模式,希望随着时间的推移它会变得更好。

在VSCode的settings.json中,添加以下内容:
emmet.includeLanguages:{“django html”:“html”}

我使用了beautify扩展,当prettier还在一行上时,该扩展马上就可以工作了。这要归功于stackoverflow

  • 将以下内容添加到settings.json
  • 安装beautify,然后将以下内容添加到settings.json
  • 重新启动VSCode以防万一

  • 您可以禁用某些语言的格式选项:

    转到Extensions,然后转到Prettier的“Extensions settings”,并为本例添加django html,如下所示


    在我的情况下,VSCode中的Prettier是罪魁祸首。我在我的
    设置.json中使用以下命令使它停止格式化

    “文件.关联”:{
    “***.html”:“html”,
    “***/templates/*/*.html”:“django html”,
    “***/templates/*”:“django txt”,
    “**/requirements{/**,*}.{txt,in}”:“pip需求”
    },    
    “[django html]”:{
    “editor.defaultFormatter”:“batisteo.vscode django”
    },
    
    files.associations
    将模板的语言模式设置为django html。
    [django html]
    设置该语言模式的设置。撰写本文时,
    batisteo.vscode django
    中的格式化程序对我没有任何帮助(因此它与
    null
    中的格式化程序相同),但我把它放在那里了,以防django扩展有。有趣的是,我甚至没有这个扩展,但问题仍然存在。如何更改文件的语言模式?在VSCode应用程序右下角的页脚中,您可以单击当前检测到的语言并进行更改。我是否需要安装特定的扩展才能看到它这个语言模式?我没有。按钮本身并没有说“语言模式”,而是说当前检测到的语言,比如“HTML”。你必须安装Django扩展。这对我很有用