Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django {% if forloop.first %} question

Tags:

python

django

I have the following code in my template:

{% for object in object_list %}
{% with game=object.game %}

{% for category in object.game.objectmeta.categories.all %}
{% if category.name|title == 'Puzzle' %}

{% if forloop.first %}
    <div class='side_header' id='dark_gamelink_side'>
        <a class='actionheader' href=""></a>
    </div>
{% endif %}

<div class='game_link' id='dark_gamelink'>
    <a class='img_link' href="{% url game_view game.id game.title|slugify %}">
        <img class='game_img' src='{{game|thumb:"78x65"}}' alt='{{game.title}}' />
    </a>
    <div class='top_game_title' style='padding:0'>
        <a style='position:relative; top:-3px' id='yellowlink' href="{% url game_view game.id game.title|slugify %}">{{game.title}} -- {{category.name|title}}</a>
        <img style='position:relative; top:1px; margin-left:12px' src='thumbsup.gif' width='17' height='18'/>
        <span style='position:relative; top:-3px; font-size:10px; color:white'>99%</span>
    </div>
    {% if game.description|length > 65 %} 
        {{ game.description|slice:"65" }}...
    {% else %}    
        {{ game.description }}
    {% endif %}
</div>

{% if forloop.counter0 == 3 %}
    <div class='more_games'><br/></div><div class='side_header' id='dark_gamelink_side'><a class='adventureheader' href=adventure.htm></a></div>
{% endif %}


{% endif %} 
{%endfor%}

{% endwith %}
{% endfor %}

Now I'm using this:

{% if forloop.first %}
    <div class='side_header' id='dark_gamelink_side'>
        <a class='actionheader' href=""></a>
    </div>
{% endif %}

to try to detect if this is the first iteration of the for loop immediately preceding it not the parent forloop. In other words I'm trying to detect if it's the 1st iteration of this for loop:

{% for category in object.game.objectmeta.categories.all %}

not this one:

{% for object in object_list %}

The way it is now isn't working because it's displaying this:

<div class='side_header' id='dark_gamelink_side'>
    <a class='actionheader' href=""></a>
</div>

Twice. How to detect the first iteration of the nested forloop?

like image 540
Jim Avatar asked May 29 '11 20:05

Jim


2 Answers

Edited:

I have never used these variables but I think forloop.parentloop.first should do it. If not blame me to have misunderstand the Django docs. ;-)

You should check if you are within the parentloop and and then within the first nested node. Please try this modified template. It should you give the right direction.

{% if forloop.parentloop.first %}     
   I am in the first loop of the parent
{% else %}
{% if forloop.first %}  
    <div class='side_header' id='dark_gamelink_side'>
        <a class='actionheader' href=""></a>
    </div>
{% endif %}
{% endif %}
like image 126
jazz Avatar answered Nov 19 '22 10:11

jazz


I think the best way to solve this isn't to detect if this is the first iteration in the loop, but rather to write your HTML so that is outside the loop entirely.

You should only be writing HTML elements in the for loop that you actually want repeated for each iteration. If that doesn't work, rethink how you're providing the data to your view (object_list, game, category, etc) so that you can write your markup more easily.

The beginning of your view will probably look something like this:

<div class='side_header' id='dark_gamelink_side'>
    <a class='actionheader' href=""></a>
</div>
{% for object in object_list %}
{% with game=object.game %}
{% for category in object.game.objectmeta.categories.all %}
{% if category.name|title == 'Puzzle' %}
like image 32
tjarratt Avatar answered Nov 19 '22 09:11

tjarratt