On Wed, Oct 5, 2011 at 3:09 PM, Tsung-Hsien <jasoniem9...@gmail.com> wrote: > I want to put pictures on website and to let each row shows only four > pictures, but I write the template which shows each pictures in the > same row. > the loop within the <tr>, I think it should include <tr> and add if > syntax to do that. > How to write the syntax? > thanks! > > {% load static %} > {% get_static_prefix as STATIC_PREFIX %} > > <table> > <tr> > {% for photo in photos %} > <td> > <img src="{{ STATIC_PREFIX }}{{photo.image}}" > width="186" > height="186" alt="" /> > <p>{{photo.title}}</p> > </td> > {% endfor %} > </tr> > </table> >
Within a for loop you have access to all sorts of counters: https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#for You can use these counters, and the divisibleby filter to work out when you need to output a new row: https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#divisibleby So roughly: {% for pic in pictures %} {% if forloop.counter0|divisibleby:"4" %}<tr>{% endif %} <td>{{ pic }}</td> {% if forloop.counter0|divisibleby:"4" %}<tr>{% endif %} {% endfor %} Cheers Tom -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.