【问题标题】:Jinja doesn't render anything when extending layout template扩展布局模板时 Jinja 不渲染任何内容
【发布时间】:2015-10-15 07:14:54
【问题描述】:

我正在尝试在页面上显示数据,但页面完全是空的。我知道数据库中有数据,并且我知道 query_db 函数返回正确的结果,但我不知道为什么 Jinja 没有呈现数据。是什么导致了这个问题?

@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>

【问题讨论】:

  • layout.html 中是否有一个名为body 的块?
  • 嗨@SeanVieira,不。让我把我拥有的东西放在我的layout.html
  • 不确定谁喜欢我的问题,但谢谢! 501分!

标签: python sqlite flask jinja2


【解决方案1】:

Jinja 不允许子模板输出任何不在父模板块中的内容。 (换句话说,块名称必须匹配。)要么将子模板中的 block body 更改为 block content,要么将 layout.html 中的 content 块重命名为 body,然后一切正常。

【讨论】:

猜你喜欢
  • 2015-11-24
  • 1970-01-01
  • 2017-11-10
  • 2020-10-04
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 2021-03-08
  • 2013-10-04
相关资源
最近更新 更多