Jinja2不渲染块 [英] Jinja2 does not render blocks

查看:60
本文介绍了Jinja2不渲染块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Flask教程,并希望使用flask创建博客.为此,我从教程中获取了一些代码,并亲自编写了一些代码.问题是,Jinja2模板引擎似乎只渲染了我在模板中声明的某些块,而我不知道为什么.

I am going through a flask tutorial and want to create a blog using flask. For this purpose I took some code from the tutorial and wrote some code myself. The problem is, that the Jinja2 templating engine only seems to render some of the blocks I declared in the templates and I don't know why.

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

This is what I got so far:

base.html:

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:

now there are the blocks, which extend the base.html:

index.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:

header.html:

{% extends "base.html" %}
{% block header %}
<!-- this is the header block -->
<h1>Microblog!</h1>
{% endblock %}

navigation.html(从另一个CSS下拉菜单教程复制):

navigation.html (copied from another css dropdown menu tutorial):

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

但是,浏览器中生成的源代码为:

However, the resulting source code in the browser is:

<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块?

What am I doing wrong? Why is only the block content from index.html rendered?

EDIT#1:说明: 预期的结果是Jinja渲染了模板和基础模板中所有提到的块,以及Jinja在通过模板/块的途中看到的所有其他块.

EDIT#1: Clarification: The expected result was Jinja rendering all mentioned blocks in the template and the base template and all others which are mentioned in the ones Jinja sees on it's way through the templates/blocks.

EDIT#2::将块放入index.html: 即使将块放入index.html,它也不会渲染它们:

EDIT#2: Putting blocks into index.html: Even if put the blocks into the index.html it does not render them:

{% 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时,Jinja2所做的就是获取基本模板(base.html),并查看index.html带来的修改-在您的情况下,更新content块.

You are implementing each block in a different html file, but you render index.html. What Jinja2 does when you tell it to render index.html is grab the base template (base.html) and look at what modification index.html brings - in your case, updating the content block.

在这种情况下,Jinja2甚至不会查看其他块实现(换句话说就是其他html文件).您想要的是实现标题/导航/等.在base.html本身中.模板引擎仅查看当前正在渲染的模板的继承链,而不会为每个渲染操作加载所有现有的模板.

Jinja2 won't even look at the other block implementations (in other words the other html files) in this case. What you want is to implement title/navigation/etc. in base.html itself. The templating engine only looks at the inheritance chain of the template you are currently rendering, it doesn't load all existing templates for each render operation.

在评论中进行了讨论,但在此处进行了更新,以防其他情况出现:如果要从不同的文件渲染不同的片段,请使用{%include <template> %}指令,而不要使用块.

Discussed in comments but updating here in case some else runs into this: if you want to render different pieces from different files, use the {%include <template> %} directive, not blocks.

这篇关于Jinja2不渲染块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆