Django模板呈现扩展标记不正确

时间:2022-09-10 19:18:30

I have three Django templates:

我有三个Django模板:

  1. base.html:
  2. base.html文件:
  3. user_links.html
  4. user_links.html
  5. user_detail.html
  6. user_detail.html

I want user_links.html to extend base.html. Next, I want user_detail.html to extend user_links.html and base.html.

我希望user_links.html扩展base.html。接下来,我希望user_detail.html扩展user_links.html和base.html。

Here's 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>

Here's user_links.html:

这是user_links.html:

{% extends "base.html" %}
Yellow
Pink
Green
{% block content %}
{% endblock %}

And here's user_detail.html

这是user_detail.html

{% extends "user_links.html" %}
{% block content %}
<h2>{{ object.username }}'s Profile</h2>
    {% if object.userprofile.bio %}
    {{ object.userprofile.bio }}
    {% endif %}
{% endblock %}

So when the browser renders user_detail.html, I want it to (i) show the stylesheet and navigation links from base.html, (ii) show the word Yellow, Pink, Green from user_links.html, (iii) and show the user's username and bio. But (ii) is not being rendered at all, though (i) and (iii) are correctly rendering.

因此,当浏览器呈现user_detail.html时,我希望它(i)显示base.html的样式表和导航链接,(ii)从user_links.html,(iii)显示单词Yellow,Pink,Green并显示用户的用户名和生物。但是(ii)根本没有渲染,尽管(i)和(iii)正确渲染。

How should the templates be set up so that I see (i), (ii) and (iii) in user_detail.html? Please advise.

应该如何设置模板,以便在user_detail.html中看到(i),(ii)和(iii)?请指教。

Note: all three templates reside in the same directory. I'm on Django 1.5

注意:所有三个模板都位于同一目录中。我在使用Django 1.5

2 个解决方案

#1


1  

If you extends a base.html template, no content not surrounded by {% block %} will be rendered at all.

如果您扩展base.html模板,则根本不会呈现未被{%block%}包围的内容。

You could create additional {% block precontnet %}{% endblock %} in base.html, and wraps Pink/Yellow/Red in user_links.html

你可以在base.html中创建额外的{%block precontnet%} {%endblock%},并在user_links.html中包装Pink / Yellow / Red

Or you can put Pink/Yellow/Red in {% block content %} if user_links.html and use {{ block.super }} in user_detail.html

或者你可以将粉红色/黄色/红色放在{%block content%}中if user_links.html并在user_detail.html中使用{{block.super}}

links.html

links.html

{% extends "base.html" %}
{% block content %}
    Yellow
    Pink
    Green
{% endblock %}

user_detail.html

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 %}

#2


0  

Place div after </p> in base.html

在base.html中将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>

Try this in user_links.html

在user_links.html中试试这个

{% extends "base.html" %}
{% block content %}
Yellow
Pink
Green
{% endblock %}

#1


1  

If you extends a base.html template, no content not surrounded by {% block %} will be rendered at all.

如果您扩展base.html模板,则根本不会呈现未被{%block%}包围的内容。

You could create additional {% block precontnet %}{% endblock %} in base.html, and wraps Pink/Yellow/Red in user_links.html

你可以在base.html中创建额外的{%block precontnet%} {%endblock%},并在user_links.html中包装Pink / Yellow / Red

Or you can put Pink/Yellow/Red in {% block content %} if user_links.html and use {{ block.super }} in user_detail.html

或者你可以将粉红色/黄色/红色放在{%block content%}中if user_links.html并在user_detail.html中使用{{block.super}}

links.html

links.html

{% extends "base.html" %}
{% block content %}
    Yellow
    Pink
    Green
{% endblock %}

user_detail.html

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 %}

#2


0  

Place div after </p> in base.html

在base.html中将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>

Try this in user_links.html

在user_links.html中试试这个

{% extends "base.html" %}
{% block content %}
Yellow
Pink
Green
{% endblock %}