Always keep a beginner's mind, don't forget the|

凌波樂

园龄:4年1个月粉丝:0关注:5

Flask中render_template('index.html')查找index.html文件

问题:jinja2.exceptions.TemplateNotFound: index.html

解决方法如下:

普通:
render_template('index.html'),其中index.html的查询是找创建app = Flask(name)文件的同级目录的templates中查找

注意:
当导入create_app函数
from RealProject import create_app
app = create_app()
@app.route('/')
def hello_world():
return render_template('index.html') # 这时index.html还是要到包含create_app函数代码文件的同级目录的templates中查找

蓝图:

video.views.py 蓝图文件

from flask import Blueprint, render_template
bp = Blueprint('blog', name, url_prefix='/blog', static_folder='static', template_folder='templates')

def index():
return render_template('index.html') # 这里的index.html也是查询该文件的同级目录的templates中查找

注意:
这里的index视图函数,即使是其他py文件在创建create_app()函数中执行app.add_url_rule('/', endpoint='index', view_func=video.hello_world),
也是从video.views.py的同级目录的templates中查找,
而且需要注册video.bp蓝图,否则也找不到index.html
from app.video import views as video
app.register_blueprint(video.bp)

本文作者:凌波樂

本文链接:https://www.cnblogs.com/spring5512/p/17763975.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   凌波樂  阅读(404)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起