Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 March 24, 2008 20:12:17

Tim S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using mod in if in template


Hi Folks,

I want to do this:

<table>
<tr>
{% for image in Images %}
<td><a href="/gallery/image/{{image.id}}/"><img src="{% thumbnail
image.image 200x100 %}"/></a><br/>
{{ image.name }}<br/>
{{ image.comment }}<br/>
</td>
{% if forloop.counter % 4 %}
</tr>
<tr>
{% endif %}
{% endfor %}
</tr>
</table>

which should hopefully give me a gallery of images where each line has four
thumbnails on it.

Unfortunately, I can't do the mod (%) in the if statement, as it's not valid
syntax. Is there a way around this, or a simpler solution?

Thanks,

Tim.

--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#2 March 24, 2008 20:13:56

Michael W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using mod in if in template


Can you simulate it with an incremental variable?

2008/3/24, Tim Sawyer <>:
>
>
> Hi Folks,
>
> I want to do this:
>
> <table>
> <tr>
> {% for image in Images %}
> <td><a href="/gallery/image/{{image.id}}/"><img src="{% thumbnail
> image.image 200x100 %}"/></a><br/>
> {{ image.name }}<br/>
> {{ image.comment }}<br/>
> </td>
> {% if forloop.counter % 4 %}
> </tr>
> <tr>
> {% endif %}
> {% endfor %}
> </tr>
> </table>
>
> which should hopefully give me a gallery of images where each line has
> four
> thumbnails on it.
>
> Unfortunately, I can't do the mod (%) in the if statement, as it's not
> valid
> syntax. Is there a way around this, or a simpler solution?
>
> Thanks,
>
> Tim.
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#3 March 24, 2008 20:20:54

James B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using mod in if in template


On Mon, Mar 24, 2008 at 2:12 PM, Tim Sawyer <> wrote:
> Unfortunately, I can't do the mod (%) in the if statement, as it's not valid
> syntax. Is there a way around this, or a simpler solution?

The first thing to do is to step back and remember that the Django
template language is not Python; completely forgetting Python syntax
is a good first step to writing effective templates, because making
assumptions that some bit of syntax will or won't work will get you in
trouble.

The second thing to do is to read the template documentation to see
what is available, because doing so will turn up things like this:http://www.djangoproject.com/documentation/templates/#divisibleby--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#4 March 24, 2008 20:29:10

Tim S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using mod in if in template


On Monday 24 Mar 2008, James Bennett wrote:
> The first thing to do is to step back and remember that the Django
> template language is not Python

I know that - writing it as python code was the quickest way to get across
what I wanted! :-)

> The second thing to do is to read the template documentation to see
> what is available, because doing so will turn up things like this:
>
>http://www.djangoproject.com/documentation/templates/#divisiblebyI saw that, but I couldn't work out how to conditionally output html based on
its value. {% if forloop.counter|divisibleby: "4" %} doesn't work.

On Monday 24 Mar 2008, Michael Wieher wrote:
> Can you simulate it with an incremental variable?

Sorry, I don't know what you mean. Can you explain more or give me a quick
example?

Thanks,

Tim.



--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#5 March 24, 2008 20:54:30

[EMAIL P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using mod in if in template


This seems to work fine for me:http://dpaste.com/41059/On Mar 24, 2:29 pm, Tim Sawyer <> wrote:
> On Monday 24 Mar 2008, James Bennett wrote:
>
> > The first thing to do is to step back and remember that the Django
> > template language is not Python
>
> I know that - writing it as python code was the quickest way to get across
> what I wanted! :-)
>
> > The second thing to do is to read the template documentation to see
> > what is available, because doing so will turn up things like this:
>
> >http://www.djangoproject.com/documentation/templates/#divisibleby>
> I saw that, but I couldn't work out how to conditionally output html based on
> its value. {% if forloop.counter|divisibleby: "4" %} doesn't work.
>
> On Monday 24 Mar 2008, Michael Wieher wrote:
>
> > Can you simulate it with an incremental variable?
>
> Sorry, I don't know what you mean. Can you explain more or give me a quick
> example?
>
> Thanks,
>
> Tim.
--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#6 March 24, 2008 21:10:06

Michael W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using mod in if in template


honestly i think forloop.counter is what you need to use.

when you say it doesn't work, what do you mean? you do realize this
variable starts at '1' not '0' ?


2008/3/24, <>:
>
>
> This seems to work fine for me:http://dpaste.com/41059/>
> On Mar 24, 2:29 pm, Tim Sawyer <> wrote:
> > On Monday 24 Mar 2008, James Bennett wrote:
> >
> > > The first thing to do is to step back and remember that the Django
> > > template language is not Python
> >
> > I know that - writing it as python code was the quickest way to get
> across
> > what I wanted! :-)
> >
> > > The second thing to do is to read the template documentation to see
> > > what is available, because doing so will turn up things like this:
> >
> > >http://www.djangoproject.com/documentation/templates/#divisibleby> >
> > I saw that, but I couldn't work out how to conditionally output html
> based on
> > its value. {% if forloop.counter|divisibleby: "4" %} doesn't work.
> >
> > On Monday 24 Mar 2008, Michael Wieher wrote:
> >
> > > Can you simulate it with an incremental variable?
> >
> > Sorry, I don't know what you mean. Can you explain more or give me a
> quick
> > example?
> >
> > Thanks,
> >
> > Tim.
> >
>

--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#7 March 24, 2008 21:23:12

Evert R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using mod in if in template


On 24 Mar 2008, at 19:29 , Tim Sawyer wrote:
>> The second thing to do is to read the template documentation to see
>> what is available, because doing so will turn up things like this:
>>
>>http://www.djangoproject.com/documentation/templates/#divisibleby>
> I saw that, but I couldn't work out how to conditionally output html
> based on
> its value. {% if forloop.counter|divisibleby: "4" %} doesn't work.

If that's your literal code, you should probably remove the space
between the colon and quote: divisibleby:"4"


--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#8 March 24, 2008 22:18:41

scott l.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using mod in if in template


On 2008-03-24, at 1312, Tim Sawyer wrote:
>
> Hi Folks,
>
> I want to do this:
>
> <table>
> <tr>
> {% for image in Images %}
> <td><a href="/gallery/image/{{image.id}}/"><img src="{% thumbnail
> image.image 200x100 %}"/></a><br/>
> {{ image.name }}<br/>
> {{ image.comment }}<br/>
> </td>
> {% if forloop.counter % 4 %}
> </tr>
> <tr>
> {% endif %}
> {% endfor %}
> </tr>
> </table>
>
> which should hopefully give me a gallery of images where each line
> has four
> thumbnails on it.
>
> Unfortunately, I can't do the mod (%) in the if statement, as it's
> not valid
> syntax. Is there a way around this, or a simpler solution?



What about just converting it to a 2-dimensional array in the view?
For example, using grouper() from the itertools recipes :

def grouper(n, iterable, padvalue=None):
"grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'),
('g','x','x')"
return izip(**n)

Images = grouper(4, Images)


Then, the logic in the template is straightforward:

<table>
{% for row in Images %}
<tr>
{% for image in row %}
<td>{% if image %} ... image detail ... {% endif %}</td>
{% endfor %}
</tr>
{% endfor %}
</table>


scott.


:http://docs.python.org/lib/itertools-recipes.html--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#9 March 24, 2008 22:32:12

Tim S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using mod in if in template


On Monday 24 Mar 2008, Evert Rol wrote:
> On 24 Mar 2008, at 19:29 , Tim Sawyer wrote:
> >> The second thing to do is to read the template documentation to see
> >> what is available, because doing so will turn up things like this:
> >>
> >>http://www.djangoproject.com/documentation/templates/#divisibleby> >
> > I saw that, but I couldn't work out how to conditionally output html
> > based on
> > its value. {% if forloop.counter|divisibleby: "4" %} doesn't work.
>
> If that's your literal code, you should probably remove the space
> between the colon and quote: divisibleby:"4"

A-ha, sorted thanks. It wasn't obvious from the documentation that I read
that you could actually use the filters inside {% %} blocks. I know better
now!

Thanks again everyone,

Tim.

--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

Board footer

Moderator control

Enjoy the 11th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support