Django {%if forloop.first%}问题 [英] Django {% if forloop.first %} question

查看:357
本文介绍了Django {%if forloop.first%}问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的模板中有以下代码:

  {%object_list%中的对象} 
{% with game = object.game%}

{%for object in object.game.objectmeta.categories.all%}
{%如果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;顶部:-3px'id ='yellowlink'href ={%url game_view game.id game.title | slugify%}> {{game.title}} - {{category.name | title}}< / A>
< img style ='position:relative;顶部:1px的; margin-left:12px'src ='thumbsup.gif'width = '17'height = '18'/>
< span style ='position:relative;顶部:-3px;字体大小:10px的;颜色:白色大于99%的LT; /跨度>
< / div>
{%如果game.description | length> 65%}
{{game.description | slice:65}} ...
{%else%}
{{game.description}}
{%endif %}
< / div>

{%ifloop.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%}

现在我使用这个:

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

尝试检测是否是第一次迭代for循环紧接在它之前不是父forloop。换句话说,我试图检测是否是这个for循环的第一次迭代:

  {%对象中的类别。 game.objectmeta.categories.all%} 

不是这个:

  {%object_list%中的对象} 

现在的方式是不正常的,因为它显示出来:

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

两次。如何检测嵌套forloop的第一次迭代?

解决方案

编辑:



我从来没有使用过这些变量,但是我认为 forloop.parentloop.first 应该这样做。如果不是怪我误解了Django docs 。 ; - )



你应该检查你是否在parentloop内,然后在第一个嵌套的节点内。请尝试这个修改后的模板。你应该给出正确的方向。

  {%if forloop.parentloop.first%} 
我在父母的第一个循环
{%else%}
{%if forloop.first%}
< div class ='side_header'id ='dark_gamelink_side'>
< a class ='actionheader'href =>< / a>
< / div>
{%endif%}
{%endif%}


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?

解决方案

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 %}

这篇关于Django {%if forloop.first%}问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆