【发布时间】:2020-01-01 05:43:33
【问题描述】:
我制作了一个自定义标签来在我的代码中设置一个标志(用于逻辑)。 我使用link 作为设置标志的参考。
这是我的自定义标签的代码:
from django import template
register = template.Library()
@register.simple_tag
def update_variable(value):
data = value
return str(data)
我的应用目录:
├── admin.py
├── apps.py
├── database_consistency.py
├── forms.py
├── __init__.py
├── models.py
├── templatetags
│ ├── __init__.py
│ └── vars.py
├── tests.py
├── urls.py
└── views.py
基本上我正在尝试在模板中设置一个标志:
<!--Diamonds:-->
<!--Initialized diamond_flag-->
{% with diamond_flag as False %}
{% for diamond in item.diamonds.all reversed %}
{% update_variable False as diamond_flag %}
{% if forloop.first and diamond.rate != 0 %}
...
<!--Trying to update the flag-->
{% update_variable "True" as diamond_flag %}
{% endif %}
{% endfor %}
{% if diamond_flag == "True" or diamond_flag == "1" or diamond_flag == 1 or diamond_flag == True %}
<td>Working</td> <!--This line is not working, the code never runs-->
<td>-</td>
{% endif %}
我希望代码之间的标志值为 True,以便以下“if”条件为真,代码相应地运行。
【问题讨论】:
标签: django templates django-templates django-filter django-template-filters