jinja2.exceptions.TemplateNotFound:home.html

时间:2019-03-03 17:24:25

标签: python flask

我查看了这个问题,发现每个人都和另一个相似:

from flask import Flask,render_template
app = Flask(__name__)



@app.route("/")
@app.route('/home')
def home():
   return render_template('home.html')


@app.route('/about')
def about():
    return render_template('about.html')

if __name__ == '__main__':
    app.run(debug=True)

错误

Traceback (most recent call last):
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\LEON\AppData\Roaming\Sublime Text 3\Packages\User\__pycache__\flaskblog.py", line 10, in home
    return render_template('home.html')
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\templating.py", line 134, in render_template
    return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\jinja2\environment.py", line 869, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\jinja2\environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\jinja2\environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\jinja2\loaders.py", line 113, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\templating.py", line 58, in get_source
    return self._get_source_fast(environment, template)
  File "C:\Users\LEON\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\templating.py", line 86, in _get_source_fast
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: home.html

1 个答案:

答案 0 :(得分:0)

函数render_template()在名为templates的文件夹中查找html模板。确保您的项目有此文件夹,并且所有html文件都放在其中。可以在here中找到有关典型Flask项目结构的更多详细信息。