Jinja doesn't render anything when extending layout template

陌路散爱 提交于 2019-12-20 05:22:15

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/31613507/jinja-doesnt-render-anything-when-extending-layout-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!