【问题标题】:Django pass object to include要包含的 Django 传递对象
【发布时间】:2011-12-17 05:33:12
【问题描述】:

我无法在 Django 中执行以下操作:

{% include "admin/includes/pager.html" with title_pager="{{myobject.title}}" %}

{% include "admin/includes/pager.html" with title_pager="{{myobject}}" %}

解决方法是什么?

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    您不需要在模板标签中将参数括在{{ }} 括号中。

    如果它是一个变量,而不是一个字符串,那么不要使用"" 引号。

    以下应该有效:

    {% include "admin/includes/pager.html" with title_pager=myobject.title %}
    
    {% include "admin/includes/pager.html" with title_pager=myobject %}
    

    有关更多信息,请参阅include tag 的 Django 文档。

    【讨论】:

    • @Bwire include 文档给出了一个包含多个变量的示例。如果您在阅读后仍然卡住,请打开一个新问题。
    • Updated link 并回答:{% include "name_snippet.html" with person="Jane" greeting="Hello" %}
    • 我的模板没有渲染,我发现原因是我把它分成了多行,以使其更具可读性。但我需要 {%include "bla.html" with var=value %} 在一行中。
    猜你喜欢
    • 2021-09-10
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    相关资源
    最近更新 更多