【问题标题】:Weird template rendering behavior with Django and links <a>Django 和链接 <a> 的奇怪模板渲染行为
【发布时间】:2017-08-05 19:01:35
【问题描述】:

我注意到,当我在模板中的 for 循环中输入一个链接时,它会被注入到第一个较低的元素中。下图说明:

基础模板:

<html>
<head></head>
<body>
{% block content %}
{% endblock %}
</body>
</html>

扩展基本模板的模板

{% extends 'base.html'%}
{% block content %}
{% if elem_list %}
<ul>
{for elem in elem_list}
<li> <a class="bob" href="">

<div class="div1">
<div class="subdiv"></div>
</div>
<div class="div2">
<div class="subdiv"></div>
</div>
</a> </li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}

以及我在页面上得到的输出

<html>
<head></head>
<body>
<ul>
<a class="bob" href="">
<li> <a class="bob" href="">

<div class="div1">
<a class="bob" href=""></a>
<div class="subdiv"><a class="bob" href=""></a></div>
<div class="subdiv"></div>
</div>
<div class="div2">
<a class="bob" href=""></a>
<div class="subdiv"><a class="bob" href=""></a></div>
<div class="subdiv"></div>
</div>

</a></li>
</ul>
</body>
</html>

这是 Django 的某种特性吗?我该如何阻止它?

【问题讨论】:

    标签: python django django-templates


    【解决方案1】:

    这与 Django 完全无关。

    您的 HTML 无效,因为像 a 这样的内联元素不能包含像 div 这样的块元素。

    您可能正在通过浏览器的开发人员工具查看生成的 HTML,该工具正在尽最大努力解释您损坏的 HTML。

    【讨论】:

    • 哦,我明白了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-06-18
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多