Search code examples
htmldjangodjango-formsbootstrap-studio

how to declare a variable in html for primary key and use it in an 'if' statement


I need to run a segment of a code when the primary key of the object is 1,4,7...etc. Basically, I need to run a if statement for a certain block of code.

            {% 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.



Solution

  • Probably this will work (using divisibleby):

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