0

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 class="container" style="background-color:white">
        <div class="row">
        <div id="section2" class="container-fluid">
                    {% include 'app/gpd_blocks/corporate_goal.html' %}
                </div>
    </div>
    <hr />
    </div>

    <div class="container" style="background-color:white">
        <div class="row">
        <div id="section5" class="container-fluid">
                    {% 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 class="row" id="super">
    <p>&nbsp</p>
</div>
<div class="row" id="super">
    <div class="col-11" style="color: ivory; font-weight: bold; font-size: 1.3em;">
        CORPORATE GOALS BLOCK
    </div>
</div>
<div class="row" 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 class="row">
        <div class="col">
            Corp. Goal: {{ goal.corp_goal_title }}
        </div>
    </div>

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


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

</ul>

<!-- Tabs content for details-->
<div class="tab-content" >
    <!--First tab-->
    <div id="det1{{ goal.id }}" class="tab-pane fade show active">
        <div class="row">
            <!--Column 1-->
            <div class="col">
                <table class="table-bordless" 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 class="row">
                    <textarea class="form-control" readonly title="Description" style="background-color:aliceblue !important;">{{goal.corp_goal_description}}</textarea>
                </div>
            </div>
            <!--Column 2-->
            <div class="col">
                <table class="table-bordless" 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 class="row">
                    <textarea class="form-control" readonly title="Comment" style="background-color:aliceblue !important;">{{goal.corp_goal_comment}}</textarea>
                </div>
            </div>
        </div>
    </div>

    <!--Second tab-->
    <div id="det2{{ goal.id }}" class="tab-pane fade" style="margin-top:20px;">
        <p>Factor for Goal Achievement:</p>
        <table class="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 class="row">
    <p>123</p>
</div>


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

{% endblock %}

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

5
  • 1
    What is the difference between corporate_goals and corporate_goal in the context? Is it a typo or a mistake? Commented Feb 18, 2022 at 10:15
  • Thanks a lot, it's my TYPO in question [have edited] Commented Feb 18, 2022 at 10:20
  • Do something like this {% for goal in corporate_goals.all %} {{ goal.corp_goal_title }} {% endfor %} Commented Feb 18, 2022 at 11:09
  • I have added .all and still nothing Commented Feb 18, 2022 at 11:25
  • when I am doing smth like that {% if corporate_goals %} True {% for goal, compl_weight in corporate_goals %} {{ goal.corp_goal_title }} {% endfor %} {% else %} False {% endif %} I have True Commented Feb 18, 2022 at 11:28

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

i have deleted all nested block content and also include, but it doesn't help
I'm still thinking that the problem is the html structure. Try to write other html template just with the forloops and some fields to test. If a forloop works once, it has to work twice. You can also see the html code of the rendered page by right-clicking in the browser and clicking "View page source code", and check if there is something wrong.
now in my form_gpd.html i have only two same include {% include 'app/gpd_blocks/corporate_goal.html' %}. In my corporate_goal.html only: {% for goal, compl_weight in corporate_goals %} <div class="row"> {{ goal.corp_goal_title }} </div> {% endfor %}. First include correctly display the value, the second one display nothing. Why, I can't understand
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.