问题
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