Home > front end >  how to loop through a condition in Django template
how to loop through a condition in Django template

Time:06-21

I'm attempting to display "you don't have active investment" just when there are no investments, however I've discovered that whenever the loop runs, it displays the same message multiple times when there is no investment.

I believe I have given enough information to anyone who is willing to give it a trial.

The code snippet is below, along with a screenshot at the bottom.

                <h1 > Active Plan</h1>
                
                {% for investment in investments %}
                    {% if investment.is_active == True %}              
                        <div >
                            <div >
                                <div >                          
                                        <div >
                                            <div>
                                                <h1  style="font-family: Open Sans;">{{investment.plan}}</h1>
                                                <div  style="font-family: Open Sans;"><span >Invested Amount -</span>  <span >{{investment.deposit_amount}} USD</span></div>
                                            </div>
                                            <div >
                                                <div>
                                                    <h1  style="font-family: Open Sans;">{{investment.created_at}}</h1>
                                                    <div  style="font-family: Open Sans;">Start Date</div>
                                                </div>
                                                <div >
                                                    <i ></i>
                                                </div>
                                                <div>
                                                    {% if investment.plan == "Basic - Daily 2% for 180 Days" %}
                                                    <h1  style="font-family: Open Sans;">{{investment.due_date}}</h1>
                                                    {% else %} 
                                                    <h1  style="font-family: Open Sans;">{{investment.due_date}}</h1>
                                                    {% endif %}
                                                    <div  style="font-family: Open Sans;">End Date</div>
                                                </div>
                                            </div>
                                        <div >
                                                <div>
                                                    {% if investment.plan == "Basic - Daily 2% for 180 Days" %}
                                                    <h1  style="font-family: Open Sans;">${{investment.investment_return}}</h1>
                                                    {% else %} 
                                                    <h1  style="font-family: Open Sans;">$ {{investment.investment_return}}</h1>
                                                    {% endif %}
                                                    <div  style="font-family: Open Sans;">Total Return</div>
                                                </div>
                                        </div>
                                            <div>
                                                {% if investment.plan == "Basic - Daily 2% for 180 Days" %}
                                                <h1  style="font-family: Open Sans;">$ {{investment.basic_interest}}</h1>
                                                {% else %}                 
                                                <h1  style="font-family: Open Sans;">$ {{investment.premium_interest}}</h1>
                                                {% endif %}
                                                <div  style="font-family: Open Sans;">Net Profit Earn</div>
                                            </div>
                                    </div>
                                </div>
                            </div>
                        </div>                
                    {% else %}
                    <p>You dont have active Investment</p>
                    {% endif %}
                {% endfor %}
                <h1 > Recently Ended</h1>
                {% for investment in investments %}
                    {% if investment.is_active == False %} 
                    <div >
                        <div >
                            <div >
                                <div >                          
                                        <div >
                                            <div>
                                                <h1  style="font-family: Open Sans;">{{investment.plan}}</h1>
                                                <div  style="font-family: Open Sans;"><span >Invested Amount -</span>  <span >{{investment.deposit_amount}} USD</span></div>
                                            </div>
                                            <div >
                                                <div>
                                                    <h1  style="font-family: Open Sans;">{{investment.created_at}}</h1>
                                                    <div  style="font-family: Open Sans;">Start Date</div>
                                                </div>
                                                <div >
                                                    <i ></i>
                                                </div>
                                                <div>
                                                    {% if investment.plan == "Basic - Daily 2% for 180 Days" %}
                                                    <h1  style="font-family: Open Sans;">{{investment.due_date}}</h1>
                                                    {% else %} 
                                                    <h1  style="font-family: Open Sans;">{{investment.due_date}}</h1>
                                                    {% endif %}
                                                    <div  style="font-family: Open Sans;">End Date</div>
                                                </div>
                                            </div>
                                        <div >
                                                <div>
                                                    {% if investment.plan == "Basic - Daily 2% for 180 Days" %}
                                                    <h1  style="font-family: Open Sans;">${{investment.investment_return}}</h1>
                                                    {% else %} 
                                                    <h1  style="font-family: Open Sans;">$ {{investment.investment_return}}</h1>
                                                    {% endif %}
                                                    <div  style="font-family: Open Sans;">Total Return</div>
                                                </div>
                                        </div>
                                            <div>
                                                {% if investment.plan == "Basic - Daily 2% for 180 Days" %}
                                                <h1  style="font-family: Open Sans;">$ {{investment.basic_interest}}</h1>
                                                {% else %}                 
                                                <h1  style="font-family: Open Sans;">$ {{investment.premium_interest}}</h1>
                                                {% endif %}
                                                <div  style="font-family: Open Sans;">Net Profit Earn</div>
                                            </div>
                                    </div>
                                </div>
                            </div>
                        </div>                                   
                    </div>
                    {% else %}
                    <p>You dont have active Investment</p>
                    {% endif %}
                {% endfor %} 

enter image description here

CodePudding user response:

Something like this ?

in your views.py:

[...]

atleast_one_active=False
atleast_one_inactive=False
for investment in investments:
    if investment.is_active:
        atleast_one_active=True
    else:
        atleast_one_inactive=True

context[
    ...
    "atleast_one_inactive":atleast_one_inactive,
    "atleast_one_active":atleast_one_active,
]

return render(...)

In your HTML:

<h1 > Active Plan</h1>
{% if atleast_one_active %}
    {% for investment in investments %}
        {% if investment.is_active == True %} 

            <div >
            [ ... ]
            </div>

        {% endif %}               
    {% endfor %}
{% else %}
    <p>You dont have active Investment</p>
{% endif %}

<h1 > Recently Ended</h1>
{% if atleast_one_inactive %}
    {% for investment in investments %}
        {% if investment.is_active == False %}

            <div >
            [ ... ]
            </div>
        {% endif %}
    {% endfor %} 
{% else %}
    <p>You dont have active Investment</p>
{% endif %}
  • Related