Django 模板渲染扩展标签不正确
Django template rendering extend tag incorrectly
我有三个 Django 模板:
- base.html:
- user_links.html
- user_detail.html
我想要 user_links.html 扩展 base.html。接下来,我希望 user_detail.html 扩展 user_links.html 和 base.html。
这里是base.html:
<head>
<title>Cool App</title>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/main.css" />
</head>
<body>
<h1>Cool App</h1>
<div class="navbar">
<p>
<a href="{% url 'home' %}">HOME</a> |
{% if user.is_authenticated %}
<a href="{% url 'logout' %}">LOGOUT</a>
{% else %}
<a href="{% url 'login' %}">LOGIN</a>
{% endif %}</p>
{% block content %}
{% endblock %}
{% block pagination %}
{% endblock %}</div>
这是user_links.html:
{% extends "base.html" %}
Yellow
Pink
Green
{% block content %}
{% endblock %}
这里是user_detail.html
{% extends "user_links.html" %}
{% block content %}
<h2>{{ object.username }}'s Profile</h2>
{% if object.userprofile.bio %}
{{ object.userprofile.bio }}
{% endif %}
{% endblock %}
因此,当浏览器呈现 user_detail.html 时,我希望它 (i) 显示来自 base.html 的样式表和导航链接,(ii) 显示来自 user_links.html, (iii) 并显示用户的用户名和简介。但是 (ii) 根本没有被渲染,尽管 (i) 和 (iii) 被正确渲染。
如何设置模板才能在 user_detail.html 中看到 (i)、(ii) 和 (iii)?请指教。
注意:所有三个模板都位于同一目录中。我在 Django 1.5
在base.html
中的</p>
之后放置div
<h1>Cool App</h1>
<div class="navbar">
<p>
<a href="{% url 'home' %}">HOME</a> |
{% if user.is_authenticated %}
<a href="{% url 'logout' %}">LOGOUT</a>
{% else %}
<a href="{% url 'login' %}">LOGIN</a>
{% endif %}</p>
</div>
在 user_links.html
试试这个
{% extends "base.html" %}
{% block content %}
Yellow
Pink
Green
{% endblock %}
如果您扩展 base.html 模板,则不会呈现任何未被 {% block %} 包围的内容。
您可以在 base.html
中创建额外的 {% block precontnet %}{% endblock %}
,并将 Pink/Yellow/Red 包装在 user_links.html
中
或者你可以在 {% block content %}
中输入 Pink/Yellow/Red if user_links.html
并在 user_detail.html
中使用 {{ block.super }}
links.html
{% extends "base.html" %}
{% block content %}
Yellow
Pink
Green
{% endblock %}
user_detail.html
{% extends "user_links.html" %}
{% block content %}
{{ block.super }}
<h2>{{ object.username }}'s Profile</h2>
{% if object.userprofile.bio %}
{{ object.userprofile.bio }}
{% endif %}
{% endblock %}
我有三个 Django 模板:
- base.html:
- user_links.html
- user_detail.html
我想要 user_links.html 扩展 base.html。接下来,我希望 user_detail.html 扩展 user_links.html 和 base.html。
这里是base.html:
<head>
<title>Cool App</title>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/main.css" />
</head>
<body>
<h1>Cool App</h1>
<div class="navbar">
<p>
<a href="{% url 'home' %}">HOME</a> |
{% if user.is_authenticated %}
<a href="{% url 'logout' %}">LOGOUT</a>
{% else %}
<a href="{% url 'login' %}">LOGIN</a>
{% endif %}</p>
{% block content %}
{% endblock %}
{% block pagination %}
{% endblock %}</div>
这是user_links.html:
{% extends "base.html" %}
Yellow
Pink
Green
{% block content %}
{% endblock %}
这里是user_detail.html
{% extends "user_links.html" %}
{% block content %}
<h2>{{ object.username }}'s Profile</h2>
{% if object.userprofile.bio %}
{{ object.userprofile.bio }}
{% endif %}
{% endblock %}
因此,当浏览器呈现 user_detail.html 时,我希望它 (i) 显示来自 base.html 的样式表和导航链接,(ii) 显示来自 user_links.html, (iii) 并显示用户的用户名和简介。但是 (ii) 根本没有被渲染,尽管 (i) 和 (iii) 被正确渲染。
如何设置模板才能在 user_detail.html 中看到 (i)、(ii) 和 (iii)?请指教。
注意:所有三个模板都位于同一目录中。我在 Django 1.5
在base.html
中的</p>
之后放置div
<h1>Cool App</h1>
<div class="navbar">
<p>
<a href="{% url 'home' %}">HOME</a> |
{% if user.is_authenticated %}
<a href="{% url 'logout' %}">LOGOUT</a>
{% else %}
<a href="{% url 'login' %}">LOGIN</a>
{% endif %}</p>
</div>
在 user_links.html
试试这个{% extends "base.html" %}
{% block content %}
Yellow
Pink
Green
{% endblock %}
如果您扩展 base.html 模板,则不会呈现任何未被 {% block %} 包围的内容。
您可以在 base.html
中创建额外的 {% block precontnet %}{% endblock %}
,并将 Pink/Yellow/Red 包装在 user_links.html
或者你可以在 {% block content %}
中输入 Pink/Yellow/Red if user_links.html
并在 user_detail.html
links.html
{% extends "base.html" %}
{% block content %}
Yellow
Pink
Green
{% endblock %}
user_detail.html
{% extends "user_links.html" %}
{% block content %}
{{ block.super }}
<h2>{{ object.username }}'s Profile</h2>
{% if object.userprofile.bio %}
{{ object.userprofile.bio }}
{% endif %}
{% endblock %}