模板呈现绝对路径时,python应用程序引擎django 1.2上的TemplateDoesNotExist
由于应用程序引擎警告 您使用的是默认的Django版本(0.96)。默认的Django版本将在不久的将来的appengine版本中更改。请调用use_library()显式选择Django版本。有关更多信息,请参阅 我在main.py的顶部添加了两行代码模板呈现绝对路径时,python应用程序引擎django 1.2上的TemplateDoesNotExist,python,django,google-app-engine,django-templates,Python,Django,Google App Engine,Django Templates,由于应用程序引擎警告 您使用的是默认的Django版本(0.96)。默认的Django版本将在不久的将来的appengine版本中更改。请调用use_library()显式选择Django版本。有关更多信息,请参阅 我在main.py的顶部添加了两行代码 from google.appengine.dist import use_library use_library("django", "1.2") 此代码导致此错误,完全破坏了我的应用程序 File "/home/adel/Software
from google.appengine.dist import use_library
use_library("django", "1.2")
此代码导致此错误,完全破坏了我的应用程序
File "/home/adel/Software/google_appengine/lib/django_1_2/django/template/loader.py", line 138, in find_template
raise TemplateDoesNotExist(name)
TemplateDoesNotExist: /home/adel/Workspace/jeeneo/site/Common/templates/en_US/template.html
我一直在谷歌上搜索,发现Django不再支持有类似相对路径问题的开发人员,在我的例子中,我使用了绝对路径,我的代码就是这样构造的
site/
Frontpage/
template/en_US/index.html
index.py
Events/
template/en_US/event.html
template/en_US/create.html
event.py
create.py
Common/
template/en_US/template.html
template.py
每个类都包含这样的模板类
from Common import Template
class Event(Template):
def get(self):
然后打电话
self.render(__file__, "event.html")
要呈现HTML文件,render函数中包含以下内容
def render(self, path, html):
self.base = self.get_base()
email = None
if self.user:
self.auth_url = users.create_logout_url("/")
email = self.user.email
else:
self.auth_url = users.create_login_url(self.request.uri)
self.template.update({
"base": self.base,
"user": self.user,
"lang": self.lang,
"template_html" : os.path.join(os.path.dirname(__file__), "templates/%s/template.html" % self.lang),
"email": email,
"auth_url": self.auth_url
})
html = os.path.join(os.path.dirname(path), "templates/%s/%s" % (self.lang, html))
self.template.update(self.data)
self.response.out.write(template.render(html, self.template))
html文件(event.html)包含以下代码
{% extends template_html %}
我有两个解决方案在我的脑海中,也许重组一切,使所有的模板在一个文件夹中,或有本地安装的Django 0.96应用程序引擎。。。但我很确定一定有一个更简单的解决方案。我遇到了一些类似的问题,我在这里介绍了一些修复方法: 使用app engine的
template.render
时,它会将template\u DIRS
设置为要渲染的模板目录。Django 1.2中的extends
标记检查所包含的模板是否在TEMPLATE\u DIRS
目录中。如果它们位于父(或同级)目录中,则会失败
我找到的解决方案是不使用AppEngine的模板.render
,而是使用Django的模板类编写自己的模板。然后,我还必须将TEMPLATE\u DIRS
设置为项目目录的根目录
这是我在1.4.2中的记忆,当我第一次研究它时,行为可能已经改变了(尽管我的变通方法仍然可以很好地工作)。请包括所有相关代码(例如,整个模板类),而不仅仅是一行代码片段。另外,它声称模板不存在的路径有效吗?@Nick我已附加完整的渲染功能。@Nickadel@adel-笔记本电脑:~/Software/google_appengine$file/home/adel/Workspace/jeeneo/site/Common/templates/en_US/template.html/home/adel/Workspace/jeeneo/site/Common/templates/en_US/template.html:html文档文本谢谢,我将在几个小时内尝试您的解决方案,并向您报告。