Python Flask 找不到模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23846927/
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
Flask unable to find templates
提问by Iamcool
My project structure is like below
我的项目结构如下
run.py
lib/
mysite/
conf/
__init__.py (flask app)
settings.py
pages/
templates/
index.html
views.py
__init__.py
This is mysite.conf.__init__
这是 mysite.conf.__init__
from flask import Flask
app = Flask(__name__)
app.debug = True
My idea is to now import app
to every other module to use it to create views. Here in this case there is a module pages
.
我的想法是现在导入app
到每个其他模块以使用它来创建视图。在这种情况下,这里有一个模块pages
。
In pages.views
I have some code like
在pages.views
我有一些代码像
from flask import render_template
from mysite.conf import app
@app.route('/')
def index():
return render_template('index.html')
The index.html
is placed in pages/templates
将index.html
被放置在pages/templates
When I run this app from run.py
which is like below
当我运行这个应用程序时run.py
,如下所示
from mysite.conf import app
app.run()
I'm getting template not found error. How to fix it? and why is this happening!
我收到找不到模板错误。如何解决?为什么会这样!
I'm basically a django guy, and facing a lot of inconvenience with importing the wsgi
object every time to create a view in every module! It kinda feels crazy -- Which in a way encourages circular imports. Is there any way to avoid this?
我基本上是一个 django 人,wsgi
每次导入对象以在每个模块中创建视图时都面临很多不便!感觉有点疯狂——这在某种程度上鼓励了循环导入。有什么办法可以避免这种情况吗?
采纳答案by Martijn Pieters
Flask expects the templates
directory to be in the same folder as the module in which it is created; it is looking for mysite/conf/templates
, notmysite/pages/templates
.
Flask 期望该templates
目录与创建它的模块位于同一文件夹中;它是在寻找mysite/conf/templates
,不是mysite/pages/templates
。
You'll need to tell Flask to look elsewhere instead:
你需要告诉 Flask 去别处寻找:
app = Flask(__name__, template_folder='../pages/templates')
This works as the path is resolved relative to the current module path.
这是因为路径是相对于当前模块路径解析的。
You cannot have per-module template directories, not without using blueprints. A common pattern is to use subdirectories of the templates
folder instead to partition your templates. You'd use templates/pages/index.html
, loaded with render_template('pages/index.html')
, etc.
你不能有每个模块的模板目录,不能不使用blueprints。一种常见的模式是使用templates
文件夹的子目录来对模板进行分区。你会使用templates/pages/index.html
,加载render_template('pages/index.html')
,等等。
The alternative is to use a Blueprint
instance per sub-module; you can give each blueprint a separate template folder, used for all views registered to that blueprint instance. Do note that all routes in a blueprint do have to start with a common prefix unique to that blueprint (which can be empty).
另一种方法是为Blueprint
每个子模块使用一个实例;您可以为每个蓝图提供一个单独的模板文件夹,用于注册到该蓝图实例的所有视图。请注意,蓝图中的所有路由都必须以该蓝图中唯一的公共前缀(可以为空)开头。
回答by PaulG
I ran in to this problem as well being brand new to Flask.
我遇到了这个问题,同时也是 Flask 的新手。
The solution for my situation was to create an __init__.py
file in the main app directory so as to turn app/
in to a module since, as Martijn Pieters
pointed out, flask expects templates
to be in the main moduledirectory by default. And to restate from his answer, you can reassign the default directory when instantiating the Flask
class, e.g. Flask(__name__, template_folder=any/relative/path/from/app/dir/or/absolute/path/youd/like)
我的情况的解决方案是__init__.py
在主 app 目录中创建一个文件,以便转入app/
一个模块,因为正如所Martijn Pieters
指出的,flask默认情况下希望templates
位于主模块目录中。并重申他的回答,您可以在实例化Flask
类时重新分配默认目录,例如Flask(__name__, template_folder=any/relative/path/from/app/dir/or/absolute/path/youd/like)
A simple directory structure should therefore look like this:
因此,一个简单的目录结构应如下所示:
app/
run.py
templates/
sample_template.html
__init__.py # <---- This was the missing file in my case.
... where app.py
might look like this:
...app.py
可能看起来像这样:
from flask import Flask, render_template
app = Flask(__name__) # or e.g. Flask(__name__, template_folder='../otherdir')
@app.route("/")
def home():
return "Hello world"
@app.route("/hello/again")
def hello_again():
return render_template("hello_again.html")
if __name__ == "__main__":
app.run()