Flask没有加载静态

时间:2017-02-11 19:20:07

标签: python flask routing

我刚开始在推特上向我推荐使用带有Python 2.7和virtualenv的Flask。它目前适用于“Hello World!"但是我不能让它来提供静态文件。目前我在 start.py

中有这个
from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def home():
    author = "Old Mother Hubbard"
    name = "Arthur Dent"
    return render_template('index.html', author=author, name=name)

if __name__ == "__main__":
    app.run()

start.py 位于项目根目录中,我在项目的 / static 子目录中有 index.html 文件根。但是,当我运行服务器进程并浏览到127.0.0.1时,我的堆栈跟踪出现了500错误:

[2017-02-11 19:11:49,564] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "start.py", line 8, in start
    return render_template('index.html', author=author, name=name)
  File "/home/peter/MouseV3/venv/local/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 "/home/peter/MouseV3/venv/local/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 "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/jinja2/environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/jinja2/environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/jinja2/loaders.py", line 113, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/flask/templating.py", line 57, in get_source
    return self._get_source_fast(environment, template)
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/flask/templating.py", line 85, in _get_source_fast
    raise TemplateNotFound(template)
TemplateNotFound: index.html
127.0.0.1 - - [11/Feb/2017 19:11:49] "GET / HTTP/1.1" 500 -

我尝试将index.html更改为./static/index.html,但这仍会导致基本相同的错误。当模板位于文件夹中时,为什么会出现TemplateNotFound错误?

我正在使用项目根目录中的python start.py启动服务器进程。

1 个答案:

答案 0 :(得分:1)

您收到错误是因为您将模板放在了错误的目录中。看看quickstart。模板和静态文件是两回事。模板通常位于templates子目录中(可以通过在App constructor中设置参数来更改。)