【问题标题】:Django TemplateSyntaxError only on live server (templates exist)Django TemplateSyntaxError 仅在实时服务器上(存在模板)
【发布时间】:2011-02-28 00:21:23
【问题描述】:

我遇到了一个奇怪的错误,它只发生在实时服务器上。我的 Django 模板目录是这样设置的

  • base.html
  • 两列-base.html
  • 投资组合
    • index.html
  • 外联网
    • base.html
    • index.html

作品集页面可以在多台机器上本地正常工作。它们从根 base.html 或 two-column-base.html 继承。但是,现在我已经将它们发布到 live box(本地机器是 Windows,live 是 Linux),当我尝试加载任何投资组合页面时,我得到一个 TemplateSyntaxError: "Caught TemplateDoesNotExist while rendering: base.html"。似乎是扩展标签在该根目录中不起作用的情况(???)。即使我在两列base.html(扩展base.html)上执行direct_to_template,我也会收到该错误。 Extranet 页面都可以完美运行,但这些模板都位于 /extranet 文件夹中并继承自 /extranet/base.html。

我检查过的可能问题:

  • 服务器上的文件权限很好
  • live box 上的模板目录是正确的(我正在使用os.path.dirname(os.path.realpath(__file__)) 让事情跨机器工作)
  • 文件存在且 /templates 目录与我的本地副本完全匹配
  • 从任何损坏的模板顶部删除 {% extends %} 块会导致模板呈现没有问题
  • 手动启动 shell 会话并在任何文件上调用 get_template 都可以,但尝试渲染它会在任何扩展模板上出现相同的异常。对 base.html 做同样的事情,它呈现完美(base.html 也通过 direct_to_template 呈现)

Webfaction 上的 Django 1.2、Python 2.6。提前道歉,因为这是我连续第三个或第四个“我在做一些愚蠢的事情”的问题。我能想到的唯一 x 因素是这是我第一次使用 Mercurial 而不是 svn。不知道我是怎么把事情搞砸的。

编辑: 一个可能的问题来源:本地机器是 Python 2.5,live 是 2.6。

这是我尝试渲染“两列-base.html”的回溯,它扩展了“base.html”。两个文件都在同一个目录下,所以如果能找到第一个,就可以找到第二个。 c 只是一个空的 Context 对象。

>>> render_to_string('two-column-base.html', c)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/projectname/webapps/django/lib/python2.6/django/template/loader.py", line 186, in render_to_string
    return t.render(context_instance)
  File "/home/projectname/webapps/django/lib/python2.6/django/template/__init__.py", line 173, in render
    return self._render(context)
  File "/home/projectname/webapps/django/lib/python2.6/django/template/__init__.py", line 167, in _render
    return self.nodelist.render(context)
  File "/home/projectname/webapps/django/lib/python2.6/django/template/__init__.py", line 796, in render
    bits.append(self.render_node(node, context))
  File "/home/projectname/webapps/django/lib/python2.6/django/template/debug.py", line 72, in render_node
    result = node.render(context)
  File "/home/projectname/webapps/django/lib/python2.6/django/template/loader_tags.py", line 103, in render
    compiled_parent = self.get_parent(context)
  File "/home/projectname/webapps/django/lib/python2.6/django/template/loader_tags.py", line 100, in get_parent
    return get_template(parent)
  File "/home/projectname/webapps/django/lib/python2.6/django/template/loader.py", line 157, in get_template
    template, origin = find_template(template_name)
  File "/home/projectname/webapps/django/lib/python2.6/django/template/loader.py", line 138, in find_template
    raise TemplateDoesNotExist(name)
TemplateSyntaxError: Caught TemplateDoesNotExist while rendering: base.html

我想知道这是否与刚刚添加到 Django 的模板缓存有关。

编辑 2(根据 lazerscience):

模板相关设置:

import os
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, 'templates'),
)

示例视图:

def project_list(request, jobs, extra_context={}):
    context = {
        'jobs': jobs,
    }
    print context
    context.update(extra_context)

    return render_to_response('portfolio/index.html', context, context_instance=RequestContext(request))

编辑:删除死链接

【问题讨论】:

  • 您是否有可能在模板路径中的某个位置出现反斜杠与正斜杠混合,这适用于 Windows 但不适用于 linux?
  • 您有没有可能发现意外的人工制品(旧模板)?当您部署到生产环境时,您是否首先删除了整个旧模板层次结构?
  • 不,没有反斜杠,但这是个好主意。什么有资格作为工件/旧模板?我正在通过 hg pull & hg update 进行部署。
  • 如果您一直遇到这个问题,您是否可以发布所有与模板相关的设置,以及两列布局的渲染调用以及模板中的扩展?
  • 完成。如果这不是您想要的或需要更多信息,请告诉我。

标签: django django-templates


【解决方案1】:

ARGH:我应该很久以前就发布扩展了。看在后人的份上,以及其他像我一样愚蠢的人,他们设法记住呼吸并弄清楚谷歌,这是在 Windows 上工作但在 Linux 上不工作的罪犯。

{% extends "two-column-base.html "%}

而不是

{% extends "two-column-base.html" %}

【讨论】:

  • 那么这个问题结束了吗?
  • 先生,先生。我还应该做些什么来关闭它吗?我不想把答案奖励给自己。
  • 我敢肯定,如果您回答自己的问题,您将不会获得赏金/代表,因此请随意将其标记为答案。
  • 看起来我做不到,所以代表就从窗口出去了。对我来说应该是个白痴。
【解决方案2】:

{% extends "two-column-base.html "%} 代替 {% extends "two-column-base.html" %}

是的,这也让我头疼!!! 到目前为止,我在服务器上完全重新安装了 Django。

谢谢

【讨论】:

    猜你喜欢
    • 2021-09-08
    • 2017-01-09
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 2017-05-21
    • 1970-01-01
    相关资源
    最近更新 更多