对于如下一个简单的例子,其中login.html放在app同级文件夹templates之下
app=Flask('main',__name__,template_folder='./templates') main=Blueprint('main',__name__)app.register_blueprint(main)@main.route('/',methods=['GET','POST']) def login(): return render_template('login.html')
该代码运行会报错:login.html无法找到
这个错误是因为我们的template_folder仍然是错误的。 对于render_template函数,它里面收到的地址是login.html , 但是由于它是经过蓝图main的路有函数进行路由的,所以,main的template_folder 没有被设置的话,它很可能就无法找到正确的template文件夹,从而找不到相应的html文件。
所以应该将main的template_folder确定下来:
app=Flask('main',__name__) main=Blueprint('main',__name__,template_folder='./templates')app.register_blueprint(main)@main.route('/',methods=['GET','POST']) def login(): return render_template('login.html')