On Fri, 2009-02-20 at 15:00 -0800, Marlun wrote:
> I'm not sure the answers I'm sending from my email is working so I'm
> reposting here on google groups.
> 
> In my case I wanted to get something like this:
> 
> <h2>January 2008</h2>
> <ul>
>     <li>Post name</li>
>     <li>Post name</li>
> </ul>
> <h2>December 2007</h2>
> <ul>
>     <li>Post name</li>
>     <li>Post name</li>
> </ul>
> 
> I can't figure out how to do it the way your are saying. I thought
> about doing:
> 
>     {% for entry in monts %}
>     {% ifchanged %}<h2>{{ entry.pub_date|date:"F Y" }}</h2>{%
> endifchanged %}
>     <ul>
>         <li>
>             <a href="{{ entry.get_absolute_url }}" title="Read the
> article titled {{ entry.title }}">{{ entry.title }}</a><span> on
> {{ entry.pub_date|date:"l jS \o\f F Y" }}</span>
>         </li>
>     </ul>
>     {% endfor %}
> 
> but that added <ul></ul> for every post like:
> 
> <ul><li>Post</li></ul>
> <ul><li>Post</li></lu>

Well, yes, because that's what you told it to do in the template: each
time around the loop include the ul, the li, the content and then close
them.

You need to close any existing "ul" element and open a new one whenever
something has changed. Except on the very first iteration, when there's
nothing to close, so you only open one. Assuming your pub_date attribute
is a proper datetime, this will do the job:

        {% for entry in months %}
           {% ifchanged entry.pub_date.month %}
              <h2> ...</h2>
              {% if forloop.first %}</ul>{% endif %}<ul>
           {% endifchanged %}
        {% endfor %}
        {% if months %}</ul>{% endif %}
        
(I may have messed that up slightly somewhere, but if you try it out,
you'll see the general idea, hopefully.)

One plausible approach that won't work, due to a bug in Django that
we'll fix shortly (in 1.1 most likely) is this:

        {% ifchanged entry.pub_date|date:"F Y" %}...{% endifchanged %}
        
That's because ifchanged (and a bunch of other tags) cannot process
filters in their argument lists. However, direct attribute and variable
access (such as entry.pub_date.month) work fine.

Hope that gives you some ideas about how to proceed.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to