【问题标题】:Python template helpPython 模板帮助
【发布时间】:2011-01-20 07:02:40
【问题描述】:

我正在使用 App Engine 的网络应用模板系统(与 django 类似但不完全相同)

通常我将静态目录 /templates/ 中的模板渲染为 跟随在我的主处理程序中:

dirname = os.path.dirname(__file__) 
template_file = os.path.join(dirname, os.path.join('templates', template_name)) 
output = template.render(template_file,template_values) 
self.response.out.write(output) 

对于我当前的应用程序,我想呈现一个包含在 细绳。我正在使用以下代码:

t = template.Template(template_string) 
c = template.Context(template_values) 
output = t.render(c) 
self.response.out.write(output) 

它呈现模板,但不呈现其中包含的“包含”标签 字符串。例如字符串模板

“你好世界 {% 包括 'helloworld.html' %}”

呈现“hello world”,但不呈现 'helloworld.html'。 我猜这与字符串不存在有关 在与“helloworld.html”相同的目录中,但我不知道如何 指定包含标签应该在 '/templates/*' 任何帮助,将不胜感激, 阿琼

【问题讨论】:

    标签: python django google-app-engine django-templates


    【解决方案1】:

    webapp 框架使用 Django 0.9.6 模板。如果您如上所述从字符串加载模板,则需要 configure the template loader 以便它可以找到从文件加载的依赖项。 Here's webapp 如何配置它们。

    【讨论】:

      【解决方案2】:
      {% include 'dirname/helloworld.html' %}
      

      应该可以!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-09
        • 2016-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-12
        相关资源
        最近更新 更多