Jinja2的问题:TemplateNotFound:index.html [英] Problems with Jinja2: TemplateNotFound: index.html

查看:559
本文介绍了Jinja2的问题:TemplateNotFound:index.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用jinja2构建我的第一个GAE应用程序。在克服了十几个小错误后,现在我坚持了这一点:

I'm trying to build my first GAE app with jinja2. After overcoming a dozen small errors, now I'm stuck with this:

Traceback (most recent call last):

File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "C:\Users\CG\Documents\udacity\HiMon\main.py", line 31, in get
    template = jinja_environment.get_template('index.html')
  File "C:\Program Files (x86)\Google\google_appengine\lib\jinja2\jinja2\environment.py", line 719, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "C:\Program Files (x86)\Google\google_appengine\lib\jinja2\jinja2\environment.py", line 693, in _load_template
    template = self.loader.load(self, name, globals)
  File "C:\Program Files (x86)\Google\google_appengine\lib\jinja2\jinja2\loaders.py", line 115, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "C:\Program Files (x86)\Google\google_appengine\lib\jinja2\jinja2\loaders.py", line 180, in get_source
    raise TemplateNotFound(template)
TemplateNotFound: index.html

这里是我的yaml文件:

Here my yaml file:

application: himother
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: .*
  script: main.app

libraries:
- name: webapp2
  version: "2.5.1"
- name: jinja2
  version: "2.6"

这里是我的代码:

Here my code:

import os
import webapp2

import jinja2

jinja_environment = jinja2.Environment(autoescape=True,
    loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))

class MainPage(webapp2.RequestHandler):
    def get(self):
        template_values = {
            'name': 'Serendipo',
            'verb': 'extremely happy'
        }

        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render(template_values))

app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)

这里是我的.html模板:

Here my .html template:

<!DOCTYPE html>
<html>
    <head>
        <title>Look Ma, I'm using Jinja!</title>
    </head>
    <body>
        Hi there - I'm {{ name }}, and I {{ verb }} programming!
    </body>
</html>

尽管有错误信息,但我有一个名为templates的文件夹,并在其中创建索引.html文件:

Despite the error message, I have a folder called "templates" and, within it, created the index.html file:

我也安装了jinja2。

I also have installed jinja2.

现在有谁知道这个错误的原因吗?

Does anyone have any idea of the cause of this error now?

推荐答案

好吧,我的错误简单而愚蠢。
我以错误的方式创建了文件index.html(这里是正确的方式)。所以,我的index.html文件确实是一个.text文件(因为我只是将它重命名为index.html而不是另存为index.html)。
感谢您的帮助,各位!

Well, my error was simple and silly. I have create the file "index.html" the wrong way (here the right way). So, my "index.html" file was indeed a ".text" file (because I just rename it to "index.html" instead of "save as" index.html"). Thanks for the help, guys!

这篇关于Jinja2的问题:TemplateNotFound:index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆