Python TemplateNotFound:index.html与谷歌应用程序引擎&;金甲2号
我正在尝试用jinja2构建我的第一个GAE应用程序。在克服了十几个小错误之后,现在我不得不面对以下问题: 回溯(最近一次呼叫最后一次): 这里是我的yaml文件:Python TemplateNotFound:index.html与谷歌应用程序引擎&;金甲2号,python,google-app-engine,templates,jinja2,Python,Google App Engine,Templates,Jinja2,我正在尝试用jinja2构建我的第一个GAE应用程序。在克服了十几个小错误之后,现在我不得不面对以下问题: 回溯(最近一次呼叫最后一次): 这里是我的yaml文件: application: himother version: 1 runtime: python27 api_version: 1 threadsafe: yes handlers: - url: /favicon\.ico static_files: favicon.ico upload: favicon\.ico -
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
现在有人知道这个错误的原因吗?您可能应该使用
webapp2\u extras
中包含的jinja2
版本
设置此设置的示例如下:
关键的区别在于,您不需要自己设置jinja2.Environment
,而是
from webapp2_extras import jinja2
jinja = jinja2.get_jinja2(app=self.app)
jinja.render_template("index.html")
您可能还需要在app.yaml
的库中包含jinja2
:
libraries:
- name: jinja2
version: "2.6"
男人。。我和你面临着同样的问题,刚刚找到了答案
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_环境”部分不需要额外的[,'模板']。您应该将其放在index.html文件字符串之前,如:
template = jinja_environment.get_template('templates/index.html')
至少对我来说是这样的(哦,我也没有使用autoescape=True部分,但我想它是可选的)
再想一想,也许你甚至可以离开[,'templates']部分,但是你需要在'index.html'前面加一个“/”,生成'/index.html',但这是另一种猜测。在使用了[1]中描述的webapp2_extras设置之后,我仍然有这个错误。我尝试了logging.info(jinja2.default\u-config)。这表明模板的未记录默认目录是app yaml dir/templates/('template_path':'templates')。除了那个,我什么都试过了。一旦你知道它,你可以重新设置它,或者让它保持原样
jinja2.default_config['template_path'] = "html"
如果您喜欢将模板放在不同的目录中,只需将其设置为空,并在呈现render\u response('module/home.html',**context)
jinja_environment=jinja2.environment(autoescape=True,
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(_文件,'templates'))
实际上,代码中的行表示您将请求定向到templates文件夹,但似乎您尚未将“index.html”存储在templates文件夹中,因此只需删除重定向或将索引文件传输到templates文件夹。Hi,@jgeewax,我在我的库部分包含“-name:jinja2 version:2.6”(事实上,它已经在那里了,我在这个问题中粘贴它时犯了一个错误)。我将阅读您指出的链接来研究jinja2版本的问题。autoescape=True
正好适合web开发介绍模板的Udacity课程
jinja2.default_config['template_path'] = "html"
jinja2.default_config['template_path'] = ""