django - 如何在 Django 模板中更新由 'with' 设置的变量

标签 django django-templates jinja2 with-statement

以下是我需要更新变量的代码。

{% with a=1 %}
{% for x in object_list %}
    {% if 'clone' in x.game and a is 1 %}
        <div class="alert alert-primary">
            <h2 class="p-3">{{ x.title }}</h2>
        </div>
        {{ a=2 }} # here is where I update it
    {% endif %}
{% endfor %}
{% endwith %}

但不是 {{ a=2 }}a 设置为 2,而是抛出以下错误:

TemplateSyntaxError at /
Could not parse the remainder: '=2' from 'a=2'

最佳答案

无法在 with 模板标签中重新分配变量。

您应该在后端( View )执行此操作,或者通过将变量加载到 JavaScript 中并执行您想要执行的操作,它可以在客户端执行。

关于django - 如何在 Django 模板中更新由 'with' 设置的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68200206/

相关文章:

python - 可能更改 Mako 的内联开始/结束语法?

javascript - 将数据从 javascript 传递到 Flask

jinja2 - 如何使用 jinja2 插入当前日期

python - Django ManyToMany 默认所有值

django - 启动 sublime 时加载语法文件时出错

python - 如何让用户在 Django 中构建自己的平面页面?

Django 。如何设置默认选项使用通用 CreateView

python - 值错误 - Python Django

Django 模板计算从现在到特定日期的天数

django - formset 有效但表单没有属性cleaned_data!