TemplateDoesNotExist on python app-engine django 1

2019-08-20 08:30发布

Due to App Engine warning

You are using the default Django version (0.96). The default Django version will change in an App Engine release in the near future. Please call use_library() to explicitly select a Django version. For more information see http://code.google.com/appengine/docs/python/tools/libraries.html#Django

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

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):

Then call

self.render(__file__, "event.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))

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

{% extends template_html %}

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.

1条回答
别忘想泡老子
2楼-- · 2019-08-20 08:46

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

App engine default Django version change

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.

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.

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).

查看更多
登录 后发表回答