扩展模板页面时的空白页面

时间:2014-01-28 06:52:51

标签: django

当我尝试将民意调查/索引页面扩展到我的主要博客/索引页面时,我得到空白页面。我收到了什么?

博客/ index.html中

  {% extends "polls/index.html" %}   
    <!doctype "html5">
    <html>
    <head>
    .....
   {% block poll %}{% endblock %}

轮询/ index.html中

{% block poll %}
    {% if latest_poll_list %}
        <ul>
            {% for poll in latest_poll_list %}
                <li><a href="{% url 'polls:detail' poll.id %}">{{ poll.question }}</a></li>
            {% endfor %}
        </ul>
    {% else %}
        <p>No polls are available.</p>
    {% endif %}
{% endblock %}

2 个答案:

答案 0 :(得分:1)

阻止代码{%block NAMEOFBLOCKTAG%}:

定义可由子模板覆盖的块。因此,此标记存在于父模板以及子模板中。在父模板中,我们在打开标签后立即关闭标签:

父模板中的

{% block NAME_OF_BLOCKTAG %}{% endNAME_OF_BLOCKTAG %}

在子模板中,将打开块标记,并在标记关闭之前写入要在该标记内表示的元素。

儿童模板中的

{% block NAME_OF_BLOCKTAG %} 
# some elements from child template
{% endNAME_OF_BLOCKTAG %}

扩展代码{%extends NAME_OF_PARENT_TEMPLETE%}

表示此模板扩展了父模板。用于文件顶部的子模板。

在您的情况下使用它:

#blog/index.html
<!doctype "html5">
<html>
<head>
.....
{% block poll %}{% endblock %}

#polls/index.html
{% extends "blog/index.html" %}
{% block poll %}
    {% if latest_poll_list %}
        <ul>
            {% for poll in latest_poll_list %}
                <li><a href="{% url 'polls:detail' poll.id %}">{{ poll.question }}</a></li>
            {% endfor %}
        </ul>
    {% else %}
        <p>No polls are available.</p>
    {% endif %}
{% endblock %}

答案 1 :(得分:0)

extends tag应该在子模板中使用,而不是在基础(父)模板中使用。


博客/ index.html中

<!doctype "html5">
<html>
<head>
.....
{% block poll %}{% endblock %}

轮询/ index.html中

{% extends "blog/index.html" %}    {# <--- #}
{% block poll %}
    {% if latest_poll_list %}
        <ul>
            {% for poll in latest_poll_list %}
                <li><a href="{% url 'polls:detail' poll.id %}">{{ poll.question }}</a></li>
            {% endfor %}
        </ul>
    {% else %}
        <p>No polls are available.</p>
    {% endif %}
{% endblock %}

请参阅Template inheritance