【发布时间】:2015-11-24 03:59:37
【问题描述】:
我正在尝试在 python 的不同文件夹中使用模板化的 html 文件,但我遇到了找不到文件的错误。例如,
index_path = os.path.join (os.path.join(os.path.dirname( __file__ ), '..', 'html'), 'index.html')
index.html 位于 html 文件夹中。我的 python 处理程序代码在 handlers 文件夹中。
.
├── app.yaml
├── handlers
├── html
项目结构在 Google App Engine 中部署后的样子?
注意:我正在使用:
from google.appengine.ext.webapp import template
template.render(index_path, {})
我根据以下建议修改了代码,仍然没有运气:
handlers/index.py:
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname('html')))
index_file = 'index.html'
template = jinja_environment.get_template(index_file)
content = template.render({})
self.response.out.write(content)
html/index.html:
来自 app.yaml:
libraries:
- name: jinja2
version: latest
【问题讨论】:
-
print(index_path)给你什么? -
类似于 ~/handlers/../html/portal.html
-
您使用的是什么模板系统?在 GAE 自带的模板框架 Jinja2 中,通常不会构建完整的文件路径,而是准备一个 loader。你在 index_path 上做了什么操作来得到“文件未找到错误”?
-
我正在使用 webapp 的默认模板:from google.appengine.ext.webapp import template template.render(index_path, template_values)
-
~/handlers/../html/portal.html之类的内容对于已部署的应用程序无效,因为该应用程序不会超出用户的主目录。
标签: python google-app-engine templates