Thanks, Tim. I got tripped up by a punctuation, and only half
understood what you were doing, but it put me on the track. Here's
what I ended up with:
future_events = Event.objects.filter(start_date__gte=now)
regions = (
'Pacific',
'Rocky Mountain',
)
events = []
for region in regions:
events.append((region,
future_events.filter(club__region=region)))
return render_to_response('clubs/events.html', {'events': events})
Then in the template, I have a simple check to see if there's anything
there:
{% for region in events %}
{% ifnotequal region.1.count 0 %}
<h3>{{ region.0 }} region</h3>
<ul class="events">
{% for event in region.1|dictsort:"start_date" %}
<li class="vevent">
blah blah blah
</li>
{% endfor %}
</ul>
{% endifnotequal%}
{% endfor %}
Carl, I may be mistaken, but I think filter is lazy?
On Jul 26, 2:45 pm, Carl Karsten <[EMAIL PROTECTED]> wrote:
> Tim Chase wrote:
> >> In my view, I have:
> >> future_events = Event.objects.filter(start_date__gte=now)
> >> pacific_events = future_events.filter(club__region='Pacific')
> >> rocky_mountain_events = future_events.filter(club__region='Rocky
> >> Mountain')
> >> southwest_events = future_events.filter(club__region='Southwest')
> >> midwest_events = future_events.filter(club__region='Midwest')
> >> central_events = future_events.filter(club__region='Central')
> >> northeast_events =
> >> future_events.filter(club__region='Northeast')
> >> southeast_events =
> >> future_events.filter(club__region='Southeast')
>
> >> return render_to_response('clubs/events.html', {'
> >> 'pacific_events': pacific_events,
> >> 'rocky_mountain_events':rocky_mountain_events,
> >> 'southwest_events':southwest_events,
> >> 'midwest_events':midwest_events,
> >> 'central_events':central_events,
> >> 'northeast_events':northeast_events,
> >> 'southeast_events':southeast_events,
> >> })
>
> >> And then in the view, I spit out:
> >> {% if pacific_events %}
> >> do stuff
> >> {% endif %}
>
> >> For each event type, ad nauseum.
>
> > Sounds like you want to do something like
>
> > future_events = Event.objects.filter(start_date__gte=now)
> > regions = (
> > 'Pacific',
> > 'Midwest',
> > # ... or load from your Region table
> > )
> > return render_to-response('clubs/events.html'), {'events': [
> > (region, future_events.filter(club__region=region)
> > for region in regions
> > ])
> > 'other_context_field': whatever,
> > })
>
> > and then in your template, you'd have something like
>
> > {% for region in events %}
> > <h3>Happenings in the {{ region.0 }} region</h3>
> > <ul>
> > {% for event in region.1 %}
> > <li>{{ event.description }}</li>
> > {% endfor %}
> > </ul>
> > {% endfor %}
>
> > Hope this helps,
>
> > -tim
>
> tim,
>
> I have a similar task, and this looks like what I need to do.
>
> few minor questions:
> missing )
> [(region, future_events.filter(club__region=region) for region in regions]
>
> guessing the missing ) goes here:
> [(region, future_events.filter(club__region=region)) for region in regions]
>
> What happens if there are no events in one of the regions?
>
> Does that cause a hit to the DB for each region?
>
> Carl K
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---