【问题标题】:jinja2.exceptions.TemplateNotFound: hello.htmljinja2.exceptions.TemplateNotFound: hello.html
【发布时间】:2018-12-02 20:36:48
【问题描述】:

我的项目结构如下:

flask-blog/      
    application.py          
    templates/         
        home.html

我的模板文件夹也被正确命名。我不明白问题出在哪里!

File "C:\Users\Aswathi\Anaconda3\lib\site-packages\flask\app.py", line 1799, in dispatch_request
  return self.view_functions[rule.endpoint](**req.view_args)  

File "C:\project\application.py", line 6, in hello
  return render_template('hello.html')

File "C:\Users\Aswathi\Anaconda3\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\Aswathi\Anaconda3\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\Aswathi\Anaconda3\lib\site-packages\jinja2\environment.py", line 830, in get_template
  return self._load_template(name, self.make_globals(globals))

File "C:\Users\Aswathi\Anaconda3\lib\site-packages\jinja2\environment.py", line 804, in _load_template
  template = self.loader.load(self, name, globals)

File "C:\Users\Aswathi\Anaconda3\lib\site-packages\jinja2\loaders.py", line 113, in load
  source, filename, uptodate = self.get_source(environment, name)

File "C:\Users\Aswathi\Anaconda3\lib\site-packages\flask\templating.py", line 58, in get_source
  return self._get_source_fast(environment, template)

File "C:\Users\Aswathi\Anaconda3\lib\site-packages\flask\templating.py", line 86, in _get_source_fast
  raise TemplateNotFound(template)

jinja2.exceptions.TemplateNotFound: hello.html

【问题讨论】:

  • 请描述您遇到的问题...
  • 我收到上述错误。找不到模板。
  • 我的意思是完整的堆栈跟踪。
  • 哦,我无法将完整的堆栈跟踪粘贴到 cmets 部分。所以我卸载了flask并使用pip再次安装,只是为了尝试。但现在它给了我错误 ImportError: cannot import name 'Flask'。当我去安装它说要求满足!呃

标签: python templates jinja2


【解决方案1】:

始终将 html 文件保存在 templates 文件夹中

【讨论】:

    【解决方案2】:

    您正在寻找模板 hello.html,但模板目录中的唯一模板是 home.html

    【讨论】:

      【解决方案3】:

      render_template 中传递home 而不是home.html 对我有用

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

      【讨论】:

        猜你喜欢
        • 2020-03-20
        • 1970-01-01
        • 1970-01-01
        • 2016-09-24
        • 2017-11-03
        • 1970-01-01
        • 2013-02-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多