我正在使用应用工厂和蓝图来模块化我的应用程序。 templates文件夹位于app包下,包含index.html模板。但是,我在查看页面时收到project/
├── app
│ ├── __init__.py
│ └── templates
│ └── index.html
└── run.py
。为什么找不到我的模板?
app/__init__.py
from flask import Flask, Blueprint, render_template
def create_app():
app = Flask('__name__')
app.register_blueprint(home)
return app
home = Blueprint('home', __name__)
@home.route('/')
def homepage():
return render_template('home/index.html')
run.py
from app import create_app
app = create_app()
app.run()
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/werkzeug/serving.py", line 267, in run_wsgi
execute(self.server.app)
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/werkzeug/serving.py", line 255, in execute
application_iter = app(environ, start_response)
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/saikrishnamohan/Projects/FlaskVueApp/dream-team/app/home/views.py", line 12, in homepage
return render_template('home/index.html', title="Welcome")
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/templating.py", line 133, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/jinja2/environment.py", line 869, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/jinja2/environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/jinja2/environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/jinja2/loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/templating.py", line 57, in get_source
return self._get_source_fast(environment, template)
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/templating.py", line 85, in _get_source_fast
raise TemplateNotFound(template)
TemplateNotFound: home/index.html
{{1}}
答案 0 :(得分:2)
您有错字:Flask('__name__')
不应该在__name__
附近加引号。
修正拼写错误,使其成为app = Flask(__name__)
。
传递给import_name
的参数Flask
让它可以找出模板文件夹之类的目录。由于字符串'__name__'
实际上并未描述您的应用的位置,因此Flask会假定当前的工作目录。但是您已经从上面的目录中运行,因此无法找到模板文件夹。