TemplateDoesNotExist在python app-engine django 1.2的同时模板渲染绝对路径 [英] TemplateDoesNotExist on python app-engine django 1.2 while template rendering absolute paths

查看:187
本文介绍了TemplateDoesNotExist在python app-engine django 1.2的同时模板渲染绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于App Engine警告

Due to App Engine warning


您正在使用默认的Django版本(0.96)。默认的Django版本在不久的将来会在App Engine版本中发生变化。请调用use_library()来显式选择一个Django版本。有关详细信息,请参阅 http://code.google.com/appengine /docs/python/tools/libraries.html#Django

我已经将两行代码添加到主.py

I have added two lines of code to top of main.py

from google.appengine.dist import use_library
use_library("django", "1.2")

此代码完全打破了我的应用程序提高此错误

This code totally broke my app raising this error

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中不再支持相对路径的类似问题,在我的情况下,我使用绝对路径,我的代码是这样构造的。

I have been googling and found developers with similar problem about relative path no longer supported in Django, in my case I used absolute path and my code is structured this way

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

每个类固有的模板类如下

Every class inherent Template class like this

from Common import Template
class Event(Template):
    def get(self):

然后调用

self.render(__file__, "event.html")

为了呈现HTML文件,渲染功能在

To render HTML file, the render function have this inside

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)有这个代码

And the html file (event.html) has this code

{% extends template_html %}

我在我的脑海里有两个解决方案,可能是重组所有的东西,并将所有模板放在一个文件夹中,或者在App Engine上安装Django 0.96,但我确定必须有一个更简单的解决方案。

I have two solutions in my mind, perhaps restructure everything and make all templates in one folder, or have local install of Django 0.96 on App Engine... but I am pretty sure there must be an easier solution.

推荐答案

我有一些类似的起诉,我在这里描述的一些修复:

I had some similar issues, with some fixes which I describe here:

App引擎默认Django版本更改

当您使用应用引擎的 template.render 它将 TEMPLATE_DIRS 设置到您要呈现的模板的目录中。 Django 1.2中的扩展标签会检查包含的模板是否位于 TEMPLATE_DIRS 目录中。如果他们在父(或兄弟)目录中,那么它将失败。

When you use app engine's template.render it sets the TEMPLATE_DIRS to the directory of the template you are rendering. The extends tag in Django 1.2 checks that the templates being included are inside the TEMPLATE_DIRS directory. If they are in a parent (or sibling) directory then it fails.

我发现的解决方案是不要使用应用引擎的模板.render ,而是使用Django的Template类写我自己的。然后我也必须将 TEMPLATE_DIRS 设置到我的项目目录的根目录。

The solution to this that I found was to not use app engine's template.render, and instead write my own using Django's Template class. I then also had to set TEMPLATE_DIRS to the root of my project directory.

这是从我的内存在1.4.2中,当我第一次看到这一点 - 行为可能已经改变(尽管我的解决方法仍然正常)。

This is going from my memory of how it was in 1.4.2, when I first looked into this - the behaviour might have changed since (although my workarounds still work fine).

这篇关于TemplateDoesNotExist在python app-engine django 1.2的同时模板渲染绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆