Home > Software design >  Django template for loop is empty in another block
Django template for loop is empty in another block

Time:02-19

I have a function in my view.py:

@login_required(login_url='login')
def gpd(request,pk):

    # get gpd by id
    current_gpd = get_gpd(pk)

    # get current campaign year                                                                     #TODO check types
    current_campaign = get_campaign(current_gpd.gpd_year)

    # get all corporate goals for current campaign
    corp_goals = CorporateGoal.objects.filter(gpd_year=current_campaign.year)

    compl_weight = []
    for goal in corp_goals:
        compl_weight.append(goal.corp_factor_weight*current_gpd.bonus.corporate_component//100)
    corporate_goals = zip(corp_goals, compl_weight)

    

    if is_manager(request)!=None:
        team = get_team(request)
        context = {'gpd':current_gpd, 
                   'corporate_goals':corporate_goals,
                   }
        return render(request, 'app/gpd_forms/form_gpd.html', context)
    else:
        context = {'gpd':current_gpd, 
                   'corporate_goals':corporate_goals, 
                   }
        return render(request, 'app/gpd_forms/form_gpd.html', context)

As you can see, in context I have corporate_goal. My form_gpd.html:

{% extends 'app/navbar/main.html' %}
{% load static %}

{% block content %}

{% include 'app/slidebar/gpd_form_slidebar.html' %}

    <div  style="background-color:white">
        <div >
        <div id="section2" >
                    {% include 'app/gpd_blocks/corporate_goal.html' %}
                </div>
    </div>
    <hr />
    </div>

    <div  style="background-color:white">
        <div >
        <div id="section5" >
                    {% include 'app/gpd_blocks/summary.html' %}
                </div>
    </div>
    <hr />
    </div>

    


</div>
    

{% endblock %}

for example in corporate block I am executing next:

{% load static %}


  

{% block content %}

<div  id="super">
    <p>&nbsp</p>
</div>
<div  id="super">
    <div  style="color: ivory; font-weight: bold; font-size: 1.3em;">
        CORPORATE GOALS BLOCK
    </div>
</div>
<div  id="super">
    <p>&nbsp</p>
</div>

{% for goal, compl_weight in corporate_goals %} 
<hr style="height:2px;border:none;color:rgb(87, 124, 161);background-color:rgb(87, 124, 161);" />

<!-- Corporate goal section-->
    <div >
        <div >
            Corp. Goal: {{ goal.corp_goal_title }}
        </div>
    </div>

    <div >
        <div >
            <div>
                {% if goal.corp_factor_rate %}
                    <p style="color:mediumspringgreen">rated</p>
                {% else %}
                    <p style="color:indianred">unrated</p>
                {% endif %}
            </div>
        </div>
        <div >
            <div style="margin-inline-start:auto">
                {{compl_weight}} % of total score 
            </div>
        </div>
    </div>


<!-- Tabs for details-->
    <ul >
    <li >
        <a  data-toggle="tab" href="#det1{{ goal.id }}">Goal Details</a></li>
    <li >
        <a  data-toggle="tab" href="#det2{{ goal.id }}">Other Details</a></li>

</ul>

<!-- Tabs content for details-->
<div  >
    <!--First tab-->
    <div id="det1{{ goal.id }}" >
        <div >
            <!--Column 1-->
            <div >
                <table  style="margin-top:20px;">
                    <tbody>
                        <tr>
                            <th>Start Date</th>
                            <td width="50"></td>
                            <td>{{ goal.start_date }}</td>
                        </tr>
                        <tr>
                            <th>Weight</th>
                            <td width="20"></td>
                            <td>{{ goal.corp_factor_weight }} %</td>
                        </tr>
                        <tr>
                            <th><p>&nbsp</p></th>
                        </tr>
                        <tr>
                            <th style="color:dimgray">Goal description</th>
                            <td width="18"></td>
                            <td></td>
                        </tr>
                    </tbody>
                </table>
                <div >
                    <textarea  readonly title="Description" style="background-color:aliceblue !important;">{{goal.corp_goal_description}}</textarea>
                </div>
            </div>
            <!--Column 2-->
            <div >
                <table  style="margin-top:20px;">
                    <tbody>
                        <tr>
                            <th>Due Date</th>
                            <td width="50"></td>
                            <td>{{ goal.end_date }}</td>
                        </tr>
                        <tr>
                            <th>Factor Rate</th>
                            <td width="50"></td>
                            <td>
                                {% if goal.corp_factor_rate %}
                                    {{ goal.corp_factor_rate }}
                                {% else %}
                                <div style="color:mediumspringgreen; font-weight: bold;">ongoing...</div>
                                {% endif %}
                            </td>
                        </tr>
                        <tr>
                            <th><p>&nbsp</p></th>
                        </tr>
                        <tr>
                            <th style="color:dimgray">Goal comment</th>
                            <td width="18"></td>
                            <td></td>
                        </tr>
                        
                    </tbody>
                </table>
                
                <div >
                    <textarea  readonly title="Comment" style="background-color:aliceblue !important;">{{goal.corp_goal_comment}}</textarea>
                </div>
            </div>
        </div>
    </div>

    <!--Second tab-->
    <div id="det2{{ goal.id }}"  style="margin-top:20px;">
        <p>Factor for Goal Achievement:</p>
        <table >
            <tbody>
                <tr>
                    <th>Factor</th>
                    <td>0</td>
                    <th>Description</th>
                    <td>{{ goal.corp_factor_0 }}</td>
                </tr>
                <tr>
                    <th>Factor</th>
                    <td>1</td>
                    <th>Description</th>
                    <td>{{ goal.corp_factor_1 }}</td>
                </tr>
                <tr>
                    <th>Factor</th>
                    <td>2</td>
                    <th>Description</th>
                    <td>{{ goal.corp_factor_2 }}</td>
                </tr>
                <tr>
                    <th>Factor</th>
                    <td>3</td>
                    <th>Description</th>
                    <td>{{ goal.corp_factor_3 }}</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<br />
<br />
<br />





{% endfor %}
{% endblock %}

And it works perfectly. But when in last block summary I want to use corporate_goals one more time - I have nothing on my page, looks like corporate_goals is not exist.

my summary.html

{% load static %}
{% block content %}

<div >
    <p>123</p>
</div>


<div >
    {% for goal, compl_weight in corporate_goals %} 
        {{ goal.corp_goal_title }}
    {% endfor %}
</div>
<div >
    <p>123</p>
</div>

{% endblock %}

Even If I copy all my code from corporate_goal.html into summary - I will have nothing. Why ?

CodePudding user response:

I think that the problem is that your html structure has no sense. You have three {% block content %}. Please, delete the content blocks of your include files (corporate_goal.html and summary.html) and check if this is the problem. I think that one of your content block is overriding the other one.

Just to clarify, when you use the "include tag" is like you were pasting the code from other file. So imagine the result. You have a block content that contains two other block contents inside it.

CodePudding user response:

My problem was in zip objects. When we are iterating through a zip object it is exhausted and you cannot iterate through it again. So, solution is corporate_goals = list(zip(corp_goals, compl_weight)). Anyway, thank you, @LaCharcaSoftware, for the advice, I've changed my structure to avoid duplication with block content.

  • Related