003:Python flask引入jinja2.exceptions.TemplateNotFound出错

代码

 1 #_*_coding:utf-8_*_
 2 from flask import Flask
 3 from flask import render_template
 4 app=Flask(__name__)
 5 @app.route('/hello')
 6 @app.route('/hello/<name>')
 7 def hello(name=None):
 8     return render_template('hello.html',name=name)
 9 
10 if __name__ == '__main__':
11     app.run(port=5001,debug=True,host='0.0.0.0')

hello.html

 1 <!doctype html>
 2 
 3 <title>Hello Sample</title>
 4 
 5 {% if name %}
 6 
 7   <h1>Hello {{ name }}!</h1>
 8 
 9 {% else %}
10 
11   <h1>Hello World!</h1>
12 
13 {%  endif  %}

使用python flask框架报错TemplateNotFound: hello.html

这种错误都是templates文件夹放错位置,将此templates文件夹放置在运行程序的文件夹中,就是说templates文件夹和运行文件位于同一级。

运行结果

扫描二维码关注公众号,回复: 8645427 查看本文章

猜你喜欢

转载自www.cnblogs.com/lisa2016/p/12202667.html
今日推荐