Google App Engine中的Django模板语法错误

1 投票
3 回答
608 浏览
提问于 2025-04-16 02:59

我尝试在本地启动我的Google App Engine应用程序,但遇到了一个Django错误,让我卡住了。

错误信息是:“TemplateSyntaxError: 模板 'base/_base.html' 无法被扩展,因为它不存在。”

我把模板放在了一个叫 /templates 的文件夹里,然后把 _base.html 和 index.html 放在 /templates/base 里。谢谢!

Emile @ proudn00b.com

错误信息:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/__init__.py", line 511, in __call__
    handler.get(*groups)
  File "/Users/emilepetrone/code/thebuswheel/main.py", line 65, in get
    outstr = template.render(temp, { 'path': path })
….

…..
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django/django/template/loader_tags.py", line 58, in get_parent
    raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent
TemplateSyntaxError: Template 'base/_base.html' cannot be extended, because it doesn't exist

提到的代码:

def get(self):

    path = self.request.path

    temp = os.path.join(
        os.path.dirname(__file__),
        'templates' + path)

    if not os.path.isfile(temp):
        temp = os.path.join(
            os.path.dirname(__file__),
            'templates/base/index.html')

    outstr = template.render(temp, { 'path': path })        
    self.response.out.write(outstr)

3 个回答

0

来自 @jps 的解决方案

把 base/_base.html 这个文件名改成 base.html,去掉子文件夹。然后在 index.html 里更新一下新的路径。

1

你可以再检查一下,确保在 TEMPLATE_DIRS 设置中,有一个路径指向你的模板的根目录。

另外,确保这个路径是一个完整的绝对路径,而不是相对于项目的路径。

0

我觉得你需要检查一下你的模板。错误信息中最重要的部分在最后:

模板 'base/_base.html' 不能被扩展,因为它不存在

你有没有一个叫 'base/_base.html' 的模板?如果没有,看看还有哪个其他的模板文件在尝试扩展它。

撰写回答