'I would like to run for loop to each of li tag and want to add content dynamically from django backend admin
I have created a navbar and content can be added from the backend but only the home nav bar shows the icon and the other navbar name is with the same icon. I want to add a different nav bar with a different icon. and want to run if, elif and else condition. please help me how to use if elif and else dynamically.
initial HTML without dynamic content
<!-- Nav Links -->
<li class="nav-item">
<a class="nav-link fs-5" href="#समाचार">
<i class="fas fa-address-book mr-1">
</i>समाचार
</a>
</li>
<li class="nav-item active">
<a class="nav-link fs-5" href="/">
<i class="fas fa-home mr-1"></i>
होमपेज
</a>
</li>
<li class="nav-item">
<a class="nav-link fs-5" href="#भिडियो">
<i class="far fa-clone mr-1">
</i>भिडियो
</a>
</li>
<li class="nav-item">
<a class="nav-link fs-5" href="#विजनेस">
<i class="far fa-calendar-alt mr-1">
</i>विजनेस
</a>
</li>
<li class="nav-item">
<a class="nav-link fs-5" href="#ग्यालरी">
<i class="far fa-chart-bar mr-1">
</i>ग्यालरी
</a>
</li>
<li class="nav-item">
<a class="nav-link fs-5" href="#प्रोफाइल">
<i class="far fa-copy mr-1">
</i>प्रोफाइल
</a>
</li>
Dynamic backend added HTML which suppose to change in order to work:
{% for i in nav_data %}
{% if forloop.first %}
<li class="nav-item active">
<a class="nav-link fs-5" href="/">
<i class="fas fa-home mr-1"></i>
{{i.name}}
</a>
</li>
{% elif forloop.first %}
<a class="nav-link fs-5" href="/">
<i class="fas fa-home mr-1"></i>
{{i.name}}
</a>
{% else %}
<li class="nav-item">
<a class="nav-link fs-5" href="#भिडियो">
<i class="far fa-clone mr-1">
</i>{{i.name}}
</a>
</li>
{% endif %}
{% endfor %}
*Note: need to use if, elif and else condition in each li and i.name should give value from backend in accending order.
Solution 1:[1]
Remove {% forloop.first %} from elif condition you are giving same conditions for both if and elif
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | sibtain |