Пав*_*нов 3 html python django django-templates
我正在使用 django 模板来渲染我的网页。在展开递归树的过程中,发生了一些奇怪的事情(并且无空格标签没有帮助)。
\n\n这是我的递归模板:
\n\n索引.html:
\n\n<ul class="Container">\n <li class="IsRoot">\n <div class="Expand">\n\n </div>\n\n <div class="Content">\n \xd0\xa1\xd0\xbe\xd0\xb4\xd0\xb5\xd1\x80\xd0\xb6\xd0\xb0\xd0\xbd\xd0\xb8\xd0\xb5\n </div>\n\n\n\n </li>\n\n {% include \'list.html\' with data=list %}\n\n</ul>\nRun Code Online (Sandbox Code Playgroud)\n\n和 list.html (作为递归部分):
\n\n<ul class="Container">\n <li class="Node ExpandClosed">\n <div class="Expand"></div>\n <div class="Content">\n <a href="/help/{{data.name}}">\n {{data.content}}\n </a>\n </div>\n {% for item in data.decendent %}\n {% include \'list.html\' with data=item %}\n {% endfor %}\n </li>\n</ul>\nRun Code Online (Sandbox Code Playgroud)\n\n结果如下:
\n\n
使用 open("index.html", \'r\').read() 作为 html 文件内容读取的文件的 html 内容被提取为文本,而不是 html:
\n\n<div id="frame" style="float:left; margin-left:310px;">\n <!DOCTYPE html>\n <html>\n <head>\n <title>hello</title>\n </head>\n <body>\n Body Great Style\n </body>\n </html>\n</div>\nRun Code Online (Sandbox Code Playgroud)\n\n而且元素之间有奇怪的空格:
\n\n
如何避免这种奇怪的行为?谢谢你!
\n{% spaceless %}仅删除标签之间的空格,而不删除文本中的空格。它在文档中进行了描述: https: //docs.djangoproject.com/en/2.0/ref/templates/builtins/#spaceless
如果您想删除文本中的空格,您可以使用自定义模板标签,如下所示https://gist.github.com/martinsvoboda/1bf965a8c6037c0fe1a88d89ea822df6。用法:
{% load nospaces %}
{% nospaces %}
<strong>
Hello
this is text
</strong>
{% endnospaces %}
Run Code Online (Sandbox Code Playgroud)
渲染图
<strong>Hello this is text</strong>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4731 次 |
| 最近记录: |