首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django 1.10模板将嵌套的HTML标记呈现在父标记之外

Django 1.10模板将嵌套的HTML标记呈现在父标记之外
EN

Stack Overflow用户
提问于 2017-09-25 19:50:51
回答 2查看 147关注 0票数 0

以下Django模板的行为使我有点困惑,这使我无法成功地设计输出样式。

也就是说,我有以下模板:

代码语言:javascript
复制
<article class="article
    {% if article.is_featured %} featured{% endif %}
    {% if not article.published %} unpublished{% endif %}">
{% if not detail_view %}

    <div class="post-preview">
        <a href="{% namespace_url 'article-detail' article.slug namespace=namespace default='' %}">
            <h2 class="post-title">
           {% render_model article "title" "" "" "striptags" %}
            </h2>
           {% if article.lead_in %}
            <h3 class="post-subtitle">
                {% if not detail_view %}
                    {% render_model article "lead_in" "" "" "truncatewords:'20'|striptags" %}
                {% else %}    
                    {% render_model article "lead_in" "" "" "striptags" %}
                {% endif %}
            </h3>
           {% endif %}
        </a>
        <p class="post-meta" style="margin-bottom: 0;"> Posted by 
            {% include "aldryn_newsblog/includes/author.html" with author=article.author %}
            on {{ article.publishing_date|date:"F d, Y" }}
        </p> 
        <p class="post-meta" style="margin: 0">
            <h4 style="display:inline-flex">Categories:</h4>
                {% for category in article.categories.all %}
                    <a href="/articles/category/{{category.name|lower}}">{{ category.name }} {% if not forloop.last %}, {% endif %}</a>
                {% endfor %}
        </p>
        <p class="post-meta" style="margin: 0">
            <h4 style="display:inline-flex">Tags:</h4>
                {% for tag in article.tag %}
                    <a href="/articles/category/{{tag.name|lower}}">{{ tag.name }} {% if not forloop.last %}, {% endif %}</a>
                {% endfor %}
        </p>
    </div>
    <hr>
{% endif %}

{% if detail_view %}
<!--    <h3>Testing template! (from article with detail_view=True)</h3> -->
        {% render_placeholder article.content language placeholder_language %}        
{% endif %}

</article>

该模板的输出大致如下所示:

代码语言:javascript
复制
<article class="article">
    <div class="post-preview">
        <a href="/articles/third-post/">
            <h2 class="post-title">
           Third Post
            </h2>  
            <h3 class="post-subtitle">
                    Third post lead-in text.
            </h3>
        </a>
        <p class="post-meta" style="margin-bottom: 0;"> Posted by 
    <a href="">      
    </a>

            on September 19, 2017
        </p> 
        <p class="post-meta" style="margin: 0">
            <h4 style="display:inline-flex">Categories:</h4>

             <a href="/articles/category/programming">Programming </a>               
        </p>
    <p class="post-meta" style="margin: 0">
        <h4 style="display:inline-flex">Tags:</h4>

    </p>

    </div>
    <hr>
</article>

尽管源HTML看起来是正确的,但浏览器将其视为如下图像所示。

我遗漏了什么?模板不正确吗?或者这是我观察到的虫子?我在Safari和Firefox上试过这个。结果是一样的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-09-25 20:07:05

不,那只是浏览器开发工具试图理解无效的HTML。

h元素不能进入p元素。

票数 1
EN

Stack Overflow用户

发布于 2017-09-25 20:06:43

看看这个答案:

)

他们对此做了更深入的解释,但基本上,浏览器认为嵌入在<p>标记中的<p>标记是非法的,并且是自动关闭标记的。使用不同的标记,它可以解决问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46413121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档