简体   繁体   English

Django包含模板问题,从0.96到1.2

[英]Django include templates problem, from 0.96 to 1.2

I using google app engine, in 0.96 I have no problem to include a template as following 我使用谷歌应用引擎,在0.96我没有问题包括如下模板

{% include "../header.html" %}

However, in 1.2 the code above not functioning?? 但是,在1.2上面的代码不起作用?

Any idea? 任何的想法?

The reason is that google.appengine.ext.webapp.template.render doesn't use any user-configurable TEMPLATE_DIRS . 原因是google.appengine.ext.webapp.template.render不使用任何用户可配置的TEMPLATE_DIRS Instead it invents its own TEMPLATE_DIRS byt taking the directory of the given template and using that at TEMPLATE_DIRS . 相反,它发明了自己的TEMPLATE_DIRS BYT取给定模板的目录,并使用在TEMPLATE_DIRS This means if you call render("foo/bar/fie") it will use foo/bar as your template directory and look up files from there. 这意味着如果你调用render("foo/bar/fie") ,它将使用foo/bar作为模板目录并从那里查找文件。

Now, the change from 0.96 til 1.2 is that the file lookup switched from using os.path.join to using django.utils._os.safe_join which does not allow escape from the base directory using ../ . 现在,从0.96到1.2的变化是文件查找从使用os.path.join切换到使用django.utils._os.safe_join ,它不允许使用../从基本目录中转义。

I don't see any obvious way around this. 我没有看到任何明显的方法。 It seems like you must call render with a file directly in your template directory and not in a subdirectory. 看起来您必须直接在模板目录中调用带有文件的render ,而不是在子目录中。

dkagedal is correct in his assessment of the problem, but there's is an easy workaround if you're not averse to monkeypatching: dkagedal在他对问题的评估中是正确的,但是如果你不反对monkeypatching,那么这是一个简单的解决方法:

try:
  # Bypass Django's safe_join for template paths since App Engine sandboxes the
  # filesystem anyway, and safe_join won't allow relative includes because
  # webapp.template always sets the template's parent directory as the "root",
  # rather than the app's real root directory.
  from django.utils import _os
  _os.safe_join = os.path.join
except ImportError:
  pass  # App is using a version of Django that doesn't use safe_join, it's OK.

It seems strange that that's not working- it is the correct syntax for the tag... 这似乎很奇怪,这是不正常的 - 这是标签的正确语法......

How is it not working? 怎么不行? Not bringing in the data at all? 根本没有引入数据? Error message? 错误信息?

Is header.html just regular the body sections of the html? header.html只是常规的html的正文部分吗? or is it a full standalone html page? 或者它是一个完整的独立HTML页面? (ie does it have html, head, body, tags etc? or just h, p, etc?) (即它有html,head,body,tags等?还是只有h,p等?)

Maybe try using the {% ssi %} tag as documented here: SSI Template Tag 也许尝试使用此处记录的{%ssi%}标记: SSI模板标记

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM