Python Jinja2不渲染块

Python Jinja2不渲染块,python,flask,jinja2,html-rendering,Python,Flask,Jinja2,Html Rendering,我正在学习flask教程,并希望使用flask创建一个博客。为此,我从教程中获取了一些代码,并自己编写了一些代码。问题是,Jinja2模板引擎似乎只呈现我在模板中声明的一些块,我不知道为什么 到目前为止,我得到的是: base.html: <html> <head> {% if title %} <title>{{ title }} - microblog</title> {% else %} <title&g

我正在学习flask教程,并希望使用flask创建一个博客。为此,我从教程中获取了一些代码,并自己编写了一些代码。问题是,Jinja2模板引擎似乎只呈现我在模板中声明的一些块,我不知道为什么

到目前为止,我得到的是:

base.html:

<html>
<head>
    {% if title %}
    <title>{{ title }} - microblog</title>
    {% else %}
    <title>Welcome to microblog</title>
    {% endif %}
</head>
<body>
    {% with messages = get_flashed_messages() %}
        {% if messages %}
        <ul>
        {% for message in messages %}
            <li>{{ message }}</li>
        {% endfor %}
        </ul>
        {% endif %}
    {% endwith %}

    <p>---</p>
    {% block header %}{% endblock %}
    <p>---</p>
    {% block navigation %}{% endblock %}
    <!-- other templates can insert themselves as block -->
    <!-- the following block ist called 'content' -->
    {% block content %}{% endblock %}
</body>
我做错了什么?为什么只呈现index.html中的块
内容

编辑#1:澄清: 预期的结果是Jinja渲染模板和基础模板中提到的所有块,以及Jinja通过模板/块看到的所有其他块

编辑#2:将块放入index.html: 即使将块放入
index.html
中,也不会呈现它们:

{% extends "base.html" %}
{% block content %}
<p>---</p>
{% block header %}{% endblock %}
<p>---</p>
{% block navigation %}{% endblock %}

<h1>Hi, {{ users.nickname }}!</h1>
{% for post in posts %}
<div><p>{{ post.author.nickname }} says: <b>{{ post.body }}</b></p></div>
{% endfor %}
{% endblock %}
{%extends“base.html”%}
{%block content%}
---

{%block头%}{%endblock%} ---

{%block导航%}{%endblock%} 嗨,{{users.昵称}}! {posts%%中的post为%s} {{post.author.nickname}说:{{post.body}

{%endfor%} {%endblock%}
您正在不同的html文件中实现每个块,但是您呈现
index.html
。当您告诉Jinja2呈现
index.html
时,它所做的是抓取基本模板(
base.html
),看看修改
index.html
会带来什么-在您的例子中,更新
内容

在本例中,Jinja2甚至不会查看其他块实现(换句话说,其他html文件)。您想要的是在
base.html
本身中实现title/navigation/etc。模板引擎只查看当前渲染的模板的继承链,它不会为每个渲染操作加载所有现有模板


编辑:在注释中讨论,但在这里进行更新,以防其他人遇到这种情况:如果要呈现来自不同文件的不同片段,请使用
{%include%}
指令,而不是块。

为什么只呈现index.html中的块内容?-你希望呈现什么?我希望它呈现所有写在模板中的块+超级模板的所有块以及它们可能“包括”的所有块,但显然不是这样。你知道我如何规避这个问题吗?即使我将它们放入
index.html
模板中,它们也不会被渲染。如果您在
index.html
中以与
内容
相同的方式实现块,它们应该被渲染。你到底在
index.html
中放了什么?我将编辑我的帖子,以说明“放入index.html”的意思。你需要将实际的块实现放在
base.html
index.html
中,而不仅仅是引用它们。基本上是将其他模板中的内容内联到这两个模板之一中。Jinja2只看到您给它渲染的模板以及继承链上的任何其他模板。即使您引用了导航块,此时它仍将其视为空白,因为它甚至不会查看
navigation.html
。请查看
{%include%}
指令。我想这是你想要的,而不是积木。
{% extends "base.html" %}
{% block header %}
<!-- this is the header block -->
<h1>Microblog!</h1>
{% endblock %}
{% extends "base.html" %}
{% block navigation %}
<nav id="nav">
<ul id="navigation">
    <li><a href="#" class="first">Home</a></li>
    <li><a href="#">Services &raquo;</a>
        <ul>
            <li><a href="#">Web Development</a></li>
            <li><a href="#">Logo Design</a></li>
            <li><a href="#">Identity & Branding &raquo;</a>
                <ul>
                    <li><a href="#">Business Cards</a></li>
                    <li><a href="#">Brochures</a></li>
                    <li><a href="#">Envelopes</a></li>
                    <li><a href="#">Flyers</a></li>
                </ul>                   
            </li>                   
            <li><a href="#">Wordpress</a></li>
        </ul>
    </li>
    <li><a href="#">Portfolio &raquo;</a>
        <ul>
            <li><a href="#">Graphic Design</a></li>
            <li><a href="#">Photography</a></li>
            <li><a href="#">Architecture</a></li>
            <li><a href="#">Calligraphy</a></li>
            <li><a href="#">Film &raquo;</a>
                <ul>
                    <li><a href="#">John Carter</a></li>
                    <li><a href="#">The Avengers</a></li>
                    <li><a href="#">The Amazing SpiderMan</a></li>
                    <li><a href="#">Madagascar 3</a></li>
                </ul>                       
            </li>
            <li><a href="#">Graffity </a></li>
        </ul>               
    </li>
    <li><a href="#">Testimonials</a></li>
    <li><a href="#">Blog</a></li>
    <li><a href="#" class="last">Contact</a></li>
</ul>
</nav>
{% endblock %}
<html>
<head>

    <title>Home - microblog</title>

</head>
<body>




    <p>---</p>

    <p>---</p>

    <!-- other templates can insert themselves as block -->
    <!-- the following block ist called 'content' -->


<h1>Hi, Miguel!</h1>

<div><p>John says: <b>Beautiful day in Portland!</b></p></div>

<div><p>Susan says: <b>The Avengers movie was so cool!</b></p></div>

<div><p>Xiaolong says: <b>Crouching Tiger Hidden Dragon, one of my favorites …</b></p></div>


</body>
</html>
from flask import render_template, flash, redirect
from app import app
from .forms import LoginForm

@app.route('/')
@app.route('/index')
def index():
users = {'nickname': 'Miguel'}  # fake user

posts = [  # fake array of posts
    { 
        'author': {'nickname': 'John'}, 
        'body': 'Beautiful day in Portland!' 
    },
    { 
        'author': {'nickname': 'Susan'}, 
        'body': 'The Avengers movie was so cool!' 
    },
    {
        'author': {'nickname': 'Xiaolong'},
        'body': 'Crouching Tiger Hidden Dragon, one of my favorites …'
    }
]

# Flask uses the jinja templating engine internally, which fills in the variables into the template.
# 1.arg: name of the template file in the templates folder
# 2. - x. arg: values for the variables we want to see in the rendered page
return render_template(
    'index.html',
    title='Home',
    users=users,
    posts=posts
)
{% extends "base.html" %}
{% block content %}
<p>---</p>
{% block header %}{% endblock %}
<p>---</p>
{% block navigation %}{% endblock %}

<h1>Hi, {{ users.nickname }}!</h1>
{% for post in posts %}
<div><p>{{ post.author.nickname }} says: <b>{{ post.body }}</b></p></div>
{% endfor %}
{% endblock %}