How to place different headings on a list in a template iteration in Django Template

{% for profile in profiles %}
    {% if forloop.counter == 6 %}
        <ul class="nav faq-tabs" role="tablist">
            <li class="nav-item">
                <a class="nav-link" id="branding-tab" data-toggle="tab" href="#branding" role="tab"
                   aria-controls="branding" aria-selected="false">Session III - Scalable platforms for Digital Education</a>
            </li>
        </ul>
    {% elif forloop.counter == 9 %}
        <ul class="nav faq-tabs" role="tablist">
            <li class="nav-item">
                <a class="nav-link" id="branding-tab" data-toggle="tab" href="#branding" role="tab"
                   aria-controls="branding" aria-selected="false">Session IV - Building a Resilient Digital Future</a>
            </li>
        </ul>
    {% endif %}

    <div class="tab-content">
        <div class="tab-pane fade show active" id="design" role="tabpanel" aria-labelledby="design-tab">
            <div id="accordionsing" class="faq faq-two pixFade">
                <div class="card">
                    <div class="card-header" id="heading10">
                        <h5>
                            <button class="btn btn-link collapsed" data-toggle="collapse"
                                    data-target="#{{profile.modal}}"
                                    aria-expanded="false" aria-controls="{{profile.modal}}">
                                {{profile.name}}
                            </button>
                        </h5>
                    </div>
                    <div id="{{profile.modal}}" class="collapse" aria-labelledby="heading10"
                         data-parent="#accordionsing">
                        <div style="display: flex;">
                            <div style="width: 40%;">
                                {% if profile.image %}
                                    <img src="{{profile.image.url}}" alt="{{speaker.alt}}"
                                         style="padding-left: 15px;">
                                {% endif %}
                                <div style="text-align: center; padding-top: 10px;">
                                    <p><b>Session(s):</b> Opening Session, Identity & Access Management</p>
                                    <a href="{{profile.linkedin}}" target="_blank"><i
                                                class="{{profile.linkedin_class}}"></i></a>
                                    <a href="{{profile.twitter}}" target="_blank"><i
                                                class="{{profile.twitter_class}}"></i></a>
                                    <a href="{{profile.facebook}}" target="_blank"><i
                                                class="{{profile.facebook_class}}"></i></a>
                                    <a href="{{profile.globe}}" target="_blank"><i
                                                class="{{profile.globe_class}}"></i></a>
                                </div>
                            </div>
                            <div style="width: 60%; text-align: left; padding-left: 20px; padding-right: 10px; ">

                                <p style="text-align: justify;">{{profile.profile|linebreaks}}</p>
                                <br>

                                <br>

                            </div>
                        </div>

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

We use the if statement to check if the current iteration is the 6th or the 9th. If it is the 6th, we display the first heading, and if it is the 9th, we display the second heading. Otherwise, we display the content for each iteration normally. Note that I have assumed that you want to wrap the content for each iteration inside a `

 

In Django templates, the forloop.counter starts at 1. So the first index number is 1.

You can access the current index of the loop using forloop.counter or forloop.counter0 where forloop.counter0 starts from 0 instead of 1.

If you need assistance with your projects feel free to email me at info@airgad.com or whatsapp Jesse stay safe!