好吧 刚刚的2篇文章都很水。。
这篇 也是继续水
在 templates 右键新建 html 文件:如 index.html (输入以下代码)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 9 {{ test }} 10 </body> 11 </html>
注意 除了{{test}} 其他 都是 自动生成。
回到 xxx.py 文件 修改 hello_world 方法:
如下:
1 #encoding=utf-8 2 from flask import Flask, render_template,jsonify,render_template_string 3 4 app = Flask(__name__) 5 6 7 @app.route('/') 8 def hello_world(): 9 #return 'Hello World!' 10 #return jsonify({'key':'hello'}) #返回json 11 #return render_template_string('<html><head></head><body>1111:{{test}}</body></html>',test="tttt") #通过拼接字符串方式 12 return render_template('index.html', test="tttttt") #普通返回 view 方式 13 14 15 if __name__ == '__main__': 16 app.run(debug=True)
如 代码 view 中{{test}} 为 打印 test,需要 后代返回 view时 传递参数test