【问题标题】:how to declare a variable in html for primary key and use it in an 'if' statement如何在 html 中为主键声明一个变量并在“if”语句中使用它
【发布时间】:2019-05-28 02:48:16
【问题描述】:

当对象的主键是 1,4,7...等时,我需要运行一段代码。 基本上,我需要为某个代码块运行 if 语句。

            {% with i = ev.pk %}
                {% if i%3 == 1 %}
                <div class="container">
                    <div class="intro"></div>
                    <div class="row articles">
                {% endif %}
            {% endwith %}
                        <div class="col-sm-6 col-md-4 item" data-aos="fade-left"><a href="#"><img class="img-fluid" src="{{ ev.image.url }}"></a>
                            <h3 class="name">{{ ev.event_title }}</h3>
                            <p class="description">{{ event.event_details }}</p><em class="float-left">{{ ev.date }}</em><em class="float-right">{{ ev.venue }}</em></div>

                    </div>
                </div>
        {% endfor %}```

The above code doesn't work, I need:
1. Declare a variable (i)
2. assign it ev.pk
3. run an if statement {% if i%3 == 1%}

I am open for suggestions if there is a better way to code this.


【问题讨论】:

    标签: html django django-forms bootstrap-studio


    【解决方案1】:

    这可能会起作用(使用divisibleby):

    {% with ev.pk as i %}
           {% if i|divisibleby:3 %}
           <div class="container">
               <div class="intro"></div>
               <div class="row articles">
           {% endif %}
    {% endwith %}
    

    【讨论】:

    • {% if forloop.counter0|divisibleby:4 %} 这个效果更好,谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-04-15
    • 2021-03-04
    • 2018-11-07
    • 1970-01-01
    • 2014-04-12
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多