Jinja扩展布局模板时不呈现任何内容 [英] Jinja doesn't render anything when extending layout template

查看:46
本文介绍了Jinja扩展布局模板时不呈现任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在页面上显示数据,但是页面完全是空的.我知道数据库中有数据,并且我知道query_db函数返回正确的结果,但是我无法弄清楚为什么Jinja无法呈现数据.是什么引起了这个问题?

I'm trying to display data on a page, but the page is completely empty. I know the database has data in it, and I know the query_db function returns the correct results, but I can't figure out why the data isn't being rendered by Jinja. What is causing this problem?

@app.route('/toto')
def toto():
    entries = query_db("select col1,col2 from toto where col1 = 'grand test'")
    return render_template('show_results.html', entries = entries)    

show_results.html:

{% extends "layout.html" %}
{% block body %}
  <ul class=entries>
    {% for entry in entries %}
    <li><h2>{{ entry }}</h2>
    <br>
    {% else %}
    <li><em>No entry here</em>
    {% endfor %}
  </ul>
{% endblock %}  

layout.html:

<html>
  <head>
    {% if title %}
    <title>{{ title }} - microblog</title>
    {% else %}
    <title>microblog</title>
    {% endif %}
  </head>
  <body>
    <div>Microblog: <a href="/index">Home</a></div>
    <hr>
    {% block content %}{% endblock %}
  </body>
</html>

推荐答案

Jinja不允许子模板输出任何不在父模板块中的东西. (换句话说,块名称必须匹配.)将子模板中的block body更改为block content或将layout.html中的content块重命名为body,一切都会正常.

Jinja doesn't let child templates output anything that isn't in a parent template block. (In other words, block names must match.) Either change block body in your child template to block content or rename your content block in layout.html to body and things will work.

这篇关于Jinja扩展布局模板时不呈现任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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