使用flask运行python时的部分输出

使用flask运行python时的部分输出,flask,jinja2,python-3.7,Flask,Jinja2,Python 3.7,我第一次使用flask,在youtube上做一些教程。我重新检查了代码,但没有解决。“for”中最后一个代码的输出不打印 Python代码: from flask import Flask, render_template app = Flask(__name__) @app.route('/') def hello(): return 'Hello, World!' @app.route('/about') def about(): return 'The About

我第一次使用flask,在youtube上做一些教程。我重新检查了代码,但没有解决。“for”中最后一个代码的输出不打印

Python代码:

from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def hello():
    return 'Hello, World!'


@app.route('/about')
def about():
    return 'The About Page'


@app.route('/blog')
def blog():
    posts = [{'title': 'Technology in 2019', 'author': 'Sreeram'},
             {'title': 'Expansion of oil in Russia', 'author': 'Bob'}]
    return render_template('blog.html', author='bob', sunny=True)


@app.route('/blog/<string:blog_id>')
def blogpost(blog_id):
    return 'This is blog post number' + (blog_id)


if __name__ == '__main__':
    app.debug = True
    app.run()
从烧瓶导入烧瓶,渲染\u模板
app=烧瓶(名称)
@应用程序路径(“/”)
def hello():
返回“你好,世界!”
@app.route(“/about”)
def about():
返回“关于页面”
@app.route(“/blog”)
def blog():
posts=[{'title':'2019年的技术','author':'Sreeram'},
{'title':'俄罗斯石油的扩张','作者':'鲍勃'}]
返回渲染模板('blog.html',author='bob',sunny=True)
@app.route(“/blog/”)
def blogpost(blog_id):
return'This is blog post number'+(blog_id)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.debug=True
app.run()
html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>blog</title>
</head>
<body>
<h2> Welcome to this blog! </h2>
<p> I am Sreeram, the author of this blog </p>
{% if sunny %} -->
<p> Today it is sunny</p> -->
{% else %} -->
<p> Today it is rainy</p> -->
{% endif %} -->

{% for post in posts %}
    <h2>{{post.title}}</h2>
    <h3>Author : {{post.author}}</h3>
{% endfor %}

</body>
</html>

博客
欢迎来到这个博客!
我是Sreeram,这个博客的作者

{%if sunny%}--> 今天天气晴朗

--> {%else%}--> 今天下雨了 {%endif%}--> {posts%%中的post为%s} {{post.title} 作者:{post.Author} {%endfor%}
我得到如下输出:


呈现模板时,您没有传递
帖子
!在
render\u模板中添加
posts=posts

像这样:

@app.route('/blog')
def blog():
    posts = [{'title': 'Technology in 2019', 'author': 'Sreeram'},
             {'title': 'Expansion of oil in Russia', 'author': 'Bob'}]
    return render_template('blog.html', author='bob', sunny=True, posts=posts)

您没有发送POST数据