【问题标题】:Why the Simple tag in django is not working?为什么 django 中的 Simple 标签不起作用?
【发布时间】: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


    【解决方案1】:

    您必须在模板中加载模板标签“vars.py”。 示例:

        {% load vars %}
        <!--Diamonds:-->
        <!--Initialized diamond_flag-->
    

    看过here

    【讨论】:

    • 我已经在模板顶部加载了变量,否则会产生错误。
    猜你喜欢
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 2018-05-05
    相关资源
    最近更新 更多