forloop.last FAIL in template.

208 views
Skip to first unread message

Sithembewena Lloyd Dube

unread,
Nov 19, 2010, 5:09:01 AM11/19/10
to django...@googlegroups.com
Hi everyone,

I have a template where Django is spitting out blog tags followed by a ',' when the tag is not the final one in the loop.

{% if not forloop.last %}, {% endif %}

Strangely, I am still seeing tag1, tag2, tag3,. How can I suppress that last ','?

--
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com

bruno desthuilliers

unread,
Nov 19, 2010, 5:51:33 AM11/19/10
to Django users


On 19 nov, 11:09, Sithembewena Lloyd Dube <zebr...@gmail.com> wrote:
> Hi everyone,
>
> I have a template where Django is spitting out blog tags followed by a ','
> when the tag is not the final one in the loop.
>
> {% if not forloop.last %}, {% endif %}
>
> Strangely, I am still seeing tag1, tag2, tag3,

This shouldn't happen, but since such an obvious bug would have been
spotted long ago, I guess you do have an "empty" element (for whatever
definition of "empty" apply here) at the end of your list or queryset.
So better to first check your data...







> --
> Regards,
> Sithembewena Lloyd Dubehttp://www.lloyddube.com

Sithembewena Lloyd Dube

unread,
Nov 19, 2010, 6:16:23 AM11/19/10
to django...@googlegroups.com
Hi Bruno,

Thanks for the idea! I checked my data and nothing seems out of the ordinary. truly stumps me.

Regards


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Sithembewena Lloyd Dube

unread,
Nov 19, 2010, 6:37:12 AM11/19/10
to django...@googlegroups.com
Bruno, you were absolutely right - the "last" tag in my loop wasn't really the last. I had this:

for tag in tags_list:
          tag.blogs_count = tag.blogpost_set.count()

So, each tag would get a blog count, and some tags had zero blogs. In the template, I was only writing out those tags that had a blog count above zero, yet the loop would iterate over the rest of the tags and not write them out.

I did a small fix in my views as follows:

for tag in tags_list:
          tag.blogs_count = tag.blogpost_set.count()

          clean_tags = []
          for tag in tags_list:
               if tag.blogs_count != 0:
                    clean_tags.append(tag)

          tags_list = clean_tags

I am now passing a list of only those tags with a blog count greater than nought.

Thanks!

On Fri, Nov 19, 2010 at 12:51 PM, bruno desthuilliers <bruno.des...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Sithembewena Lloyd Dube

unread,
Nov 19, 2010, 6:52:25 AM11/19/10
to django...@googlegroups.com
for tag in tags_list:
   tag.blogs_count = tag.blogpost_set.count()

clean_tags = []
for tag in tags_list:
   if tag.blogs_count != 0:
        clean_tags.append(tag)

tags_list = clean_tags

- Just fixed that. The indentation gave the code a different meaning :).
Reply all
Reply to author
Forward
0 new messages