Jinja2 不渲染块
Jinja2 does not render blocks
我正在学习 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>
现在有块,扩展 base.html:
index.html:
{% extends "base.html" %}
{% block content %}
<h1>Hi, {{ users.nickname }}!</h1>
{% for post in posts %}
<div><p>{{ post.author.nickname }} says: <b>{{ post.body }}</b></p></div>
{% endfor %}
{% endblock %}
header.html:
{% extends "base.html" %}
{% block header %}
<!-- this is the header block -->
<h1>Microblog!</h1>
{% endblock %}
navigation.html(从另一个css下拉菜单教程中复制):
{% extends "base.html" %}
{% block navigation %}
<nav id="nav">
<ul id="navigation">
<li><a href="#" class="first">Home</a></li>
<li><a href="#">Services »</a>
<ul>
<li><a href="#">Web Development</a></li>
<li><a href="#">Logo Design</a></li>
<li><a href="#">Identity & Branding »</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 »</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 »</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>
我的views.py
是这样的:
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
)
我做错了什么?为什么只渲染 index.html 中的块 content
?
编辑#1: 说明:
预期的结果是 Jinja 渲染模板和基本模板中提到的所有块,以及 Jinja 在 templates/blocks.
中看到的所有其他块。
EDIT#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 %}
您在不同的 html 文件中实现每个块,但您渲染 index.html
。当您告诉 Jinja2 渲染 index.html
时,它所做的是获取基本模板 (base.html
) 并查看 index.html
带来的修改 - 在您的情况下,更新 content
块.
Jinja2 在这种情况下甚至不会查看其他块实现(换句话说,其他 html 文件)。你想要的是实现title/navigation/etc。在 base.html
本身。模板引擎只查看您当前正在渲染的模板的继承链,它不会为每个渲染操作加载所有现有模板。
编辑: 在评论中进行了讨论,但在此处进行了更新以防其他人遇到此问题:如果您想从不同的文件中渲染不同的片段,请使用 {%include <template> %}
指令,不是方块。
我正在学习 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>
现在有块,扩展 base.html:
index.html:
{% extends "base.html" %}
{% block content %}
<h1>Hi, {{ users.nickname }}!</h1>
{% for post in posts %}
<div><p>{{ post.author.nickname }} says: <b>{{ post.body }}</b></p></div>
{% endfor %}
{% endblock %}
header.html:
{% extends "base.html" %}
{% block header %}
<!-- this is the header block -->
<h1>Microblog!</h1>
{% endblock %}
navigation.html(从另一个css下拉菜单教程中复制):
{% extends "base.html" %}
{% block navigation %}
<nav id="nav">
<ul id="navigation">
<li><a href="#" class="first">Home</a></li>
<li><a href="#">Services »</a>
<ul>
<li><a href="#">Web Development</a></li>
<li><a href="#">Logo Design</a></li>
<li><a href="#">Identity & Branding »</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 »</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 »</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>
我的views.py
是这样的:
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
)
我做错了什么?为什么只渲染 index.html 中的块 content
?
编辑#1: 说明: 预期的结果是 Jinja 渲染模板和基本模板中提到的所有块,以及 Jinja 在 templates/blocks.
中看到的所有其他块。EDIT#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 %}
您在不同的 html 文件中实现每个块,但您渲染 index.html
。当您告诉 Jinja2 渲染 index.html
时,它所做的是获取基本模板 (base.html
) 并查看 index.html
带来的修改 - 在您的情况下,更新 content
块.
Jinja2 在这种情况下甚至不会查看其他块实现(换句话说,其他 html 文件)。你想要的是实现title/navigation/etc。在 base.html
本身。模板引擎只查看您当前正在渲染的模板的继承链,它不会为每个渲染操作加载所有现有模板。
编辑: 在评论中进行了讨论,但在此处进行了更新以防其他人遇到此问题:如果您想从不同的文件中渲染不同的片段,请使用 {%include <template> %}
指令,不是方块。