Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Jinja2的问题:TemplateNotFound:index.html_Python_Google App Engine_Templates_Jinja2 - Fatal编程技术网

Python Jinja2的问题:TemplateNotFound:index.html

Python Jinja2的问题:TemplateNotFound:index.html,python,google-app-engine,templates,jinja2,Python,Google App Engine,Templates,Jinja2,我正在尝试用jinja2构建我的第一个GAE应用程序。在克服了十几个小错误之后,现在我不得不面对以下问题: 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

我正在尝试用jinja2构建我的第一个GAE应用程序。在克服了十几个小错误之后,现在我不得不面对以下问题:

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

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)
下面是my.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>

看,妈,我在用金甲!
你好,我是{{name}},我是{{{verb}}编程!
尽管有错误消息,但我有一个名为“templates”的文件夹,其中创建了index.html文件:

我还安装了jinja2


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

基于我与Jinja2的第一次GAE工作,我有两个想法。首先,在yaml文件中,根据我看到的教程,您有“-url:”,尽管我使用了“-url:/”。但是,这可能与您的问题无关。其次,我使用了本页上关于如何建立Jinja2渲染器的指导,并且在应用程序目录的templates子目录中找到模板时没有问题: 尝试使用

loader=jinja2.FileSystemLoader('templates')
而不是

loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates'))

这对我很有用。

嗯,我的错误很简单,也很愚蠢。 我用错误的方法(正确的方法)创建了文件“index.html”。因此,我的“index.html”文件实际上是一个“.text”文件(因为我只是将其重命名为“index.html”而不是“.text”)。 谢谢你们的帮助,伙计们

这为我解决了这个问题:

mkdir templates
echo "hello world" > templates/index.html

我收到了同样的错误,并尝试了所有答案。后来,我了解到,您首先需要使用正确的文件夹配置环境来搜索html文件,只有这样jinja2才能找到该文件

以下行将删除错误:

env = Environment(loader=FileSystemLoader(searchpath='C:\Folder\of\html\file')
template = env.get_template('Name_of_file.html')

您好,@v0hkus,谢谢您的回答,但错误依然存在:文件“C:\Program Files(x86)\Google\Google\appengine\lib\jinja2\jinja2\loaders.py”,第180行,在get_source raise TemplateNotFound(template)TemplateNotFound:index.html INFO 2012-06-17 14:20:15908 dev\u appserver.py:2891]“get/HTTP/1.1”500-再次感谢!出于某种原因,我实际上不得不删除这些文件,然后重新添加它们,以便Flask检测到这些文件存在。最初一定是创建了错误的模板。只需添加一个名为templates的文件夹并将index.html放在其中。虽然此代码可以作为解决方案,但最好添加上下文并解释代码。这有助于未来的访问者深入了解代码中需要注意的重要内容,并帮助其他人学习,以便他们能够将这些知识应用到自己的代码中。或者说明此代码与OP尝试的不同之处,并对比此解决方案的工作原理。这将保持高质量,不鼓励“给我代码”问题,并鼓励分享知识和信息。访问者更有可能关注和阅读信息性文章。高质量的答案更有可能获得“优待票”
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),autoescape = True)