【发布时间】:2022-01-03 19:02:51
【问题描述】:
我正在编写一个 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 %}
...保存后变成这个:
{% 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 格式化程序?
【问题讨论】:
标签: python django visual-studio-code django-templates