【问题标题】:TemplateNotFound: index.html with Google App Engine & Jinja2TemplateNotFound:带有 Google App Engine 和 Jinja2 的 index.html
【发布时间】:2012-06-13 12:43:29
【问题描述】:

我正在尝试使用 jinja2 构建我的第一个 GAE 应用程序。在克服了十几个小错误之后,现在我陷入了困境:

Traceback(最近一次调用最后一次):

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 文件:

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"

这是我的代码:

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 模板:

<!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”的文件夹,并在其中创建了 index.html 文件:

我也安装了jinja2。

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

【问题讨论】:

    标签: python google-app-engine templates jinja2


    【解决方案1】:

    您可能应该使用包含在webapp2_extras 中的jinja2 版本。

    获取此设置的示例在这里:http://webapp-improved.appspot.com/api/webapp2_extras/jinja2.html#webapp2_extras.jinja2.Jinja2

    主要区别在于,您无需自己设置jinja2.Environment,而是...

    from webapp2_extras import jinja2
    jinja = jinja2.get_jinja2(app=self.app)
    jinja.render_template("index.html")
    

    您可能还需要在app.yamllibraries 部分中包含jinja2

    libraries:                                                                      
    - name: jinja2                                                                  
      version: "2.6" 
    

    【讨论】:

    • 嗨,@jgeewax,我在我的库部分中包含了“- name: jinja2 version: 2.6”(实际上,它已经存在了,我在这个问题中粘贴它时出错了)。我将阅读您指向的链接来研究 jinja2 版本问题。
    【解决方案2】:

    伙计..我遇到了和你一样的问题,刚刚找到了答案。

    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))
    

    “jinja_environment”部分不需要额外的 [, 'templates']。这应该放在 index.html 文件字符串之前,例如:

    template = jinja_environment.get_template('templates/index.html')
    

    至少这对我来说是这样的(哦,我也没有使用 autoescape=True 部分,但我猜它是可选的)。

    再想一想,也许你甚至可以留下 [, 'templates'] 部分,但是你需要在 'index.html' 之前放一个“/”,形成 '/index.html',但这是另一个猜测。

    【讨论】:

    • autoescape=True 正好适合介绍模板的 Web 开发 udacitity 课程
    【解决方案3】:

    使用 [1] 和此处描述的 webapp2_extras 设置后,我仍然遇到此错误。我试过logging.info(jinja2.default_config)。这表明模板的未记录默认目录是 app-yaml-dir/templates/ ('template_path': 'templates')。除了那个,我什么都试过了。一旦你知道它,你可以重置它,或者让它保持原样。

    jinja2.default_config['template_path'] = "html"
    

    如果你喜欢将模板放在各种目录中,只需将其设置为空并在渲染时使用完整路径render_response('module/home.html', **context)

    jinja2.default_config['template_path'] = ""
    
    1. http://webapp-improved.appspot.com/api/webapp2_extras/jinja2.html#webapp2_extras.jinja2.Jinja2

    【讨论】:

      【解决方案4】:

      jinja_environment = jinja2.Environment(autoescape=True, loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates'))) 实际上,代码中的行意味着您将请求定向到模板文件夹,但似乎您没有将“index.html”存储在模板文件夹中,因此只需删除重定向或将索引文件传输到模板文件夹即可。

      【讨论】:

        猜你喜欢
        • 2012-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-22
        • 1970-01-01
        • 2013-02-07
        • 2014-01-20
        • 1970-01-01
        相关资源
        最近更新 更多