如何从pythonflask中的不同目录引用html模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31002890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reference a html template from a different directory in python flask
提问by BigBoy
@app.route('/view', methods=['GET', 'POST'])
def view_notifications():
posts = get_notifications()
return render_template("frontend/src/view_notifications.html", posts=posts)
So in my project/backend/src/app.pythere's this code. How would I reference the template that's in project/frontend/src/view_notifications.htmlI've tried using ..but it keeps saying the path isn't found. Is there another way I should be doing this?
所以在我的project/backend/src/app.py有这个代码。我将如何引用project/frontend/src/view_notifications.html我尝试使用的模板,..但它一直说找不到路径。还有另一种方式我应该这样做吗?
[Tue Jun 23 12:56:02.597207 2015] [wsgi:error] [pid 2736:tid 140166294406912] [remote 10.0.2.2:248] TemplateNotFound: frontend/src/view_notifications.html
[Tue Jun 23 12:56:05.508462 2015] [mpm_event:notice] [pid 2734:tid 140166614526016] AH00491: caught SIGTERM, shutting down
回答by kylie.a
Flask is looking in templates/frontend/src/view_notifications.htmlfor your template file. You either need to move your templates file to that location or change the default template folder.
Flask 正在寻找templates/frontend/src/view_notifications.html您的模板文件。您需要将模板文件移动到该位置或更改默认模板文件夹。
According to the Flask docs you can specify a different folder for your templates. It defaults to templates/in the root of your app:
根据 Flask 文档,您可以为模板指定不同的文件夹。它默认templates/在您的应用程序的根目录中:
import os
from flask import Flask
template_dir = os.path.abspath('../../frontend/src')
app = Flask(__name__, template_folder=template_dir)
UPDATE:
更新:
After testing it myself on a Windows machine the templates folder does need to be named templates. This is the code I used:
在 Windows 机器上自己测试后,模板文件夹确实需要命名为templates. 这是我使用的代码:
import os
from flask import Flask, render_template
template_dir = os.path.dirname(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
template_dir = os.path.join(template_dir, 'frontend')
template_dir = os.path.join(template_dir, 'templates')
# hard coded absolute path for testing purposes
working = 'C:\Python34\pro\frontend\templates'
print(working == template_dir)
app = Flask(__name__, template_folder=template_dir)
@app.route("/")
def hello():
return render_template('index.html')
if __name__ == "__main__":
app.run(debug=True)
With this structure:
使用这种结构:
|-pro
|- backend
|- app.py
|- frontend
|- templates
|- index.html
Changing any instance of 'templates'to 'src'and renaming the templates folder to 'src'resulted in the same error OP received.
更改'templates'to 的任何实例'src'并将模板文件夹重命名以'src'导致 OP 收到相同的错误。
回答by Macilias
a batter solution is to just go directly without os.path.abspath like this:
一个面糊的解决方案是直接去没有 os.path.abspath 这样的:
from flask import Flask
app = Flask(__name__, template_folder='../../frontend/src')
回答by urirot
Answer here miss /templates at the end. by default, the template_folder is 'templates' so:
最后在这里回答miss /templates。默认情况下,template_folder 是 'templates' 所以:
from flask import Flask
app = Flask(__name__, template_folder='../../frontend/src/templates')

