Django包含模板,覆盖“自”变量吗?

时间:2020-02-12 15:13:15

标签: python django wagtail

我受命在Django应用中修复模板结构。我对Django不太熟悉,因为我主要处理React中构建的前端项目。

Django应用程序正在使用Wagtail作为后端CMS。我遍历页面,包括一个模板块,并尝试在with上使用include语句传递变量。我可以成功地将变量传递到模板块,但是当模板块正在寻找self.something时,我需要覆盖'self'变量。

这是我的模板的样子。

{% load static wagtailimages_tags wagtailcore_tags %}

<div class="c-card {% if self.variant %}c-card--{{ self.variant }}{% endif %}">
    {% if self.image %}
        {% image self.image original class="c-card__image" loading="lazy" %}
    {% endif %}

    {% if self.title %}
        <h3 class="c-card__title">{{ self.title }}</h3>
    {% endif %}

    {% if self.text %}
        <p class="c-card__content">{{ self.text }}</p>
    {% endif %}

    {% if self.button.title and self.button.url %}
        {% include './button_block.html' with button=self.button %}
    {% endif %}
</div>

然后是循环,并包含代码段...

{% for chapter_menu in page.chapters %}
    <div class="medium-6 large-4 columns">
        {{ chapter_menu.value.title }}
        {% include './blocks/card_block.html' with self=chapter_menu.value %}
    </div>
{%  endfor %}

执行此self=chapter_menu.value会导致TypeError,这是我两次定义self时所期望的。 push() got multiple values for argument 'self'

我该怎么做?除了基本的django模板文档之外,我还能阅读任何文档吗? https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#with

1 个答案:

答案 0 :(得分:1)

添加only属性以包括...。在任何文档中均未找到该属性...

{% include './blocks/card_block.html' with self=chapter_menu.value only %}

相关问题