我使用 Flask 并尝试显示我的 html 文件,但是,我总是收到 404。我的 html 文件已经在我的模板文件夹中。这是下面的项目结构:
projectfolder/
app.py
templates/
frontend.html
这是代码:
import sys
sys.path.append('C:\Users\Software\Anaconda2\Lib\site-packages')
from flask import Flask, render_template
app = Flask(__name__)
if __name__ == "__main__":
app.run(debug=True)
@app.route('/')
def home():
return render_template('frontend.html')
最佳答案
我认为您将 app.run()
调用太早了,我建议您将其移至文件末尾。
import sys
sys.path.append('C:\Users\Software\Anaconda2\Lib\site-packages')
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('frontend.html')
if __name__ == "__main__":
app.run(debug=True)
在您的代码中,app.run
在注册任何路由之前执行。
关于python - 为什么 render_template 给出 404 not find ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42870922/