flask 出现 TemplateNotFound的问题

from flask import Flask, render_template, request
app = Flask(__name__,template_folder='./templates')

@app.route('/')
def student():
    return render_template('student.html')

@app.route('/result',methods = ['POST', 'GET'])
def result():
    if request.method == 'POST':
        result = request.form
        return render_template("result.html",result = result)

if __name__ == '__main__':
    app.run(debug = True)

出现了flask TemplateNotFound这个问题,原因在于Flask这个对象一个项目中只能创建一个,把它放到了一个__init__.py文件中,如下

app = Flask(__name__,static_folder="",static_url_path=""),结果发现出现了这个问题,原来,创建的时候,没有

template_folder这个属性,所以默认是templates这个文件夹,由于__init___.py文件的目录与templates没有在统一目录下,所以找不到。
我们在定义这个时,改为

app = Flask(__name__, template_folder='../templates',static_folder="",static_url_path="")

其中 template_folder后面的是相对位置,这样就找到这个目录了。

猜你喜欢

转载自www.cnblogs.com/test404/p/10324156.html