Django built-in: cycle

28 views
Skip to first unread message

Mark Cuevas

unread,
Jun 5, 2016, 5:40:33 PM6/5/16
to Django users
I'm using the following, which works, but doesn't break properly.  I'd like three across for each row.  How can I accomplish that?

{% for director in directors %}
<tr class="{% cycle 'odd' 'even' %}">
<td>
{{ director.full_name }}
</td>
</tr>
{% endfor %}

Mike Dewhirst

unread,
Jun 5, 2016, 7:40:43 PM6/5/16
to django...@googlegroups.com
On 6/06/2016 7:26 AM, Mark Cuevas wrote:
> I'm using the following, which works, but doesn't break properly. Â I'd
> like three across for each row. Â How can I accomplish that?
>
> {% for director in directors %}
> <tr class="{% cycle 'odd' 'even' %}">
> <td>
> {{ director.full_name }}
> </td>
> </tr>
> {% endfor %}

You need to pad out your list or queryset of directors so it comes to
mod(3) = 0

Use &nbsp; for the one or two extras

Then you can have three <td width="33%"> cells per row.

Mike


>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/14552fd8-57ca-402f-8bd8-02a634acddef%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/14552fd8-57ca-402f-8bd8-02a634acddef%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Mike Dewhirst

unread,
Jun 5, 2016, 7:52:44 PM6/5/16
to django...@googlegroups.com
On 6/06/2016 9:39 AM, Mike Dewhirst wrote:
> On 6/06/2016 7:26 AM, Mark Cuevas wrote:
>> I'm using the following, which works, but doesn't break properly. Â I'd
>> like three across for each row. Â How can I accomplish that?
>>
>> {% for director in directors %}
>> <tr class="{% cycle 'odd' 'even' %}">
>> <td>
>> {{ director.full_name }}
>> </td>
>> </tr>
>> {% endfor %}

But I forgot the "break properly"

<table>
<tr>
{% for item in synonyms.synonym_qs %}
<td width="33%">{{ item.name }}</td>
{% if forloop.last %}
</tr>
{% endif %}
{% if forloop.counter|divisibleby:3 %}
</tr><tr>
{% endif %}
{% endfor %}
</table>

You can see I made a synonyms class and populated it with a queryset.
Reply all
Reply to author
Forward
0 new messages