Re: There's got to be a better way

2007-07-27 Thread [EMAIL PROTECTED]

>
> Creating a template tag [1] might be a good idea here, especially if
> you will be displaying a list of events on any other pages.
>
> In your view, you would have:
>
> future_events =
> Event.objects.filter(start_date__gte=now).sort_by('start_date')
> return render_to_response('clubs/events.html', {'events':
> future_events})
>
> And in the template, you would have:
>
> {% show_events events "Pacific" %}
> {% show_events events "Central" %}
> ...
>
> Or take out some more duplication:
>
> view:
>
> future_events =
> Event.objects.filter(start_date__gte=now).sort_by('start_date')
> regions = ["Pacific", "Central", ... ]
> return render_to_response('clubs/events.html', {
> 'events': future_events,
> 'regions': regions})
>
> template:
>
> {% for region in regions %}
> {% show_events events region %}
> {% endfor %}
>
> Now, you can do what you want with your newly created show_events
> template tag.  You could make the region optional, for example, and
> display all events in the passed QuerySet if no region is specified.
>
> Also, if you aren't doing anything more in your view than passing the
> couple of context variables to the template, you could even get rid of
> your view entirely by using the direct_to_template generic view [2]
> instead.
>
> [1]http://www.djangoproject.com/documentation/templates_python/#writing-...
> [2]http://www.djangoproject.com/documentation/generic_views/#django-view...
>
> Gary

Thanks Gary, but it's just one page, and a fairly specialized one...
although I left it out of the above code, there's also a form to
submit events, and the template creates calendars for them.


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



Re: There's got to be a better way

2007-07-27 Thread Gary Wilson

On Jul 26, 1:22 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
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. It works, but I know I'm being stupid
> here and thoroughly violating the DRY principle. Could someone show me
> the light?

Creating a template tag [1] might be a good idea here, especially if
you will be displaying a list of events on any other pages.

In your view, you would have:

future_events =
Event.objects.filter(start_date__gte=now).sort_by('start_date')
return render_to_response('clubs/events.html', {'events':
future_events})

And in the template, you would have:

{% show_events events "Pacific" %}
{% show_events events "Central" %}
...

Or take out some more duplication:

view:

future_events =
Event.objects.filter(start_date__gte=now).sort_by('start_date')
regions = ["Pacific", "Central", ... ]
return render_to_response('clubs/events.html', {
'events': future_events,
'regions': regions})

template:

{% for region in regions %}
{% show_events events region %}
{% endfor %}

Now, you can do what you want with your newly created show_events
template tag.  You could make the region optional, for example, and
display all events in the passed QuerySet if no region is specified.

Also, if you aren't doing anything more in your view than passing the
couple of context variables to the template, you could even get rid of
your view entirely by using the direct_to_template generic view [2]
instead.

[1] 
http://www.djangoproject.com/documentation/templates_python/#writing-custom-template-tags
[2] 
http://www.djangoproject.com/documentation/generic_views/#django-views-generic-simple-direct-to-template

Gary


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



Re: There's got to be a better way

2007-07-26 Thread [EMAIL PROTECTED]

>
> I second the weirdness.  I'd try tossing an
>
>   assert False, len(future_events)
>
> immediately after the
>
>   future_events = Event.objects.select_related().filter(...)
>
> statement to see what Django thinks it's bringing back for future
> events.
>
> > It wasn't anything to do with region, which isn't another table, it's
> > just a field under Club with choices (since they'll basically never
> > change)
>
> ah...helpful to know.  So from what I've discerned from your
> models, you have something like
>
> class Club(Model):
>   REGIONS = (
> ('NE', 'NorthEast'),
> ('PAC', 'Pacific'),
> #...
> )
>   region = CharField(..., choices=Club.REGIONS)
>   name = CharField(...)
>
> class Event(Model):
>   club = ForeignKey(Club) # FK or M2M?
>   start_date = DateField(...)
>
> Anything important I'm missing here?
>
> I'm not sure why it's not behaving properly with the
> select_related, as that should allow you to pull back the whole
> bunch in one query (as opposed to performing a query for each event).
>
> -tim

Yup, that's more or less it. event.club is a FK.

Definitely odd, but when I put the select related in, it only finds 4
future events instead of the 13. WIthout it, I'm fine. I can't figure
out where or why it decided to quit at 4, but I'm not sure in this
case it really matters. Since region isn't another table, I'm not sure
what I'd gain by select_related (which admittedly, I don't completely
understand).
But hey, it's working, and it's MILES better than what I had. Thanks
for all your help!


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



Re: There's got to be a better way

2007-07-26 Thread Tim Chase

>> events = {} # or SortedDict if order matters
>> +  new_lists = 0
>> +  appended_lists = 0
>> for event in future_events:  # one DB hit here
>> - region = event.club.region
>> + region = str(event.club.region)
>> +if region in events:
>> +  appended_lists += 1
>> +else:
>> +  new_lists += 1
>>   ev_list = events.get(region, [])
>>   ev_list.append(event)
>>   events[region] = ev_list
>> +   assert False, '%i created, %i updated, %i total' % (
>> + new_lists, appended_lists, len(future_events))
> 
> Interesting... as long as select_related was on there, it only picked
> up 4 events. Remove it, and it gets all 13. Weird.

I second the weirdness.  I'd try tossing an

  assert False, len(future_events)

immediately after the

  future_events = Event.objects.select_related().filter(...)

statement to see what Django thinks it's bringing back for future
events.

> It wasn't anything to do with region, which isn't another table, it's
> just a field under Club with choices (since they'll basically never
> change)

ah...helpful to know.  So from what I've discerned from your
models, you have something like

class Club(Model):
  REGIONS = (
('NE', 'NorthEast'),
('PAC', 'Pacific'),
#...
)
  region = CharField(..., choices=Club.REGIONS)
  name = CharField(...)

class Event(Model):
  club = ForeignKey(Club) # FK or M2M?
  start_date = DateField(...)

Anything important I'm missing here?

I'm not sure why it's not behaving properly with the
select_related, as that should allow you to pull back the whole
bunch in one query (as opposed to performing a query for each event).

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



Re: There's got to be a better way

2007-07-26 Thread [EMAIL PROTECTED]

On Jul 26, 4:17 pm, Tim Chase <[EMAIL PROTECTED]> wrote:
> > AssertionError: {'Southeast': [],
> > 'Northeast':
> > [], 'Pacific': [ > show>],
> > 'Southwest': []}
>
> [aside:  please use edited-down inline posting conventions rather
> than top-posting to make the conversation easier to follow]
>
> I'm not sure if just using "region" is being considered a unique
> item (or being overly unique).  Try changing this line:
>
> region = event.club.region
>
> to
>
> region = str(event.club.region)
>
> or
>
> region = event.club.region.name
>
> or something that is string.  For debugging purposes, you can
> also adjust the code to look something like
>
> events = {} # or SortedDict if order matters
> +  new_lists = 0
> +  appended_lists = 0
> for event in future_events:  # one DB hit here
> - region = event.club.region
> + region = str(event.club.region)
> +if region in events:
> +  appended_lists += 1
> +else:
> +  new_lists += 1
>   ev_list = events.get(region, [])
>   ev_list.append(event)
>   events[region] = ev_list
> +   assert False, '%i created, %i updated, %i total' % (
> + new_lists, appended_lists, len(future_events))
>
> This should give an idea as to how many regions are added to the
> dictionary, and how many resultant lists should have been updated
> (the two tallies should sum to the len() of the future_events).
> You can try it with and without the "region =
> str(event.club.region)" change and see if they're different.
>
> -tim

Interesting... as long as select_related was on there, it only picked
up 4 events. Remove it, and it gets all 13. Weird.

It wasn't anything to do with region, which isn't another table, it's
just a field under Club with choices (since they'll basically never
change)


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



Re: There's got to be a better way

2007-07-26 Thread Tim Chase

> AssertionError: {'Southeast': [],
> 'Northeast':
> [], 'Pacific': [ show>],
> 'Southwest': []}

[aside:  please use edited-down inline posting conventions rather 
than top-posting to make the conversation easier to follow]

I'm not sure if just using "region" is being considered a unique 
item (or being overly unique).  Try changing this line:

region = event.club.region

to

region = str(event.club.region)

or

region = event.club.region.name

or something that is string.  For debugging purposes, you can 
also adjust the code to look something like



events = {} # or SortedDict if order matters
+  new_lists = 0
+  appended_lists = 0
for event in future_events:  # one DB hit here
- region = event.club.region
+ region = str(event.club.region)
+if region in events:
+  appended_lists += 1
+else:
+  new_lists += 1
  ev_list = events.get(region, [])
  ev_list.append(event)
  events[region] = ev_list
+   assert False, '%i created, %i updated, %i total' % (
+ new_lists, appended_lists, len(future_events))


This should give an idea as to how many regions are added to the 
dictionary, and how many resultant lists should have been updated 
(the two tallies should sum to the len() of the future_events). 
You can try it with and without the "region = 
str(event.club.region)" change and see if they're different.

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



Re: There's got to be a better way

2007-07-26 Thread [EMAIL PROTECTED]

AssertionError: {'Southeast': [],
'Northeast':
[], 'Pacific': [],
'Southwest': []}


On Jul 26, 3:45 pm, Tim Chase <[EMAIL PROTECTED]> wrote:
> >>future_events = Event.object.filter(
> >>  start_date__gte=now).select_related(
> >>  ).order_by('start_date')
>
> >>events = {} # or SortedDict if order matters
> >>for event in future_events:  # one DB hit here
> >>  region = event.club.region
> >>  ev_list = events.get(region, [])
> >>  ev_list.append(event)
> >>  events[region] = ev_list
> >>return render_to_response(..., {
> >>  'events': events, # this might need to
> >> # be events.iteritems() or something
> >> # like that
> >>  ...})
>
> > I'm probably doing something wrong in my template now, but that
> > version is only returning one event per region, and the order is
> > backwards. I tried .order_by('-start_date'), but it didn't appear to
> > make any difference.
>
> (fixed top-posting)
>
> Not having your models, I've only been guessing at them which may
> be causing problems.
>
> Before the "return render_to_response(...)" line, you might want
> to try adding one of the following
>
>assert False, repr(events)
>assert False, repr(events.items())
>
> to see what comes back.
>
> -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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: There's got to be a better way

2007-07-26 Thread Tim Chase

>>future_events = Event.object.filter(
>>  start_date__gte=now).select_related(
>>  ).order_by('start_date')
>>
>>events = {} # or SortedDict if order matters
>>for event in future_events:  # one DB hit here
>>  region = event.club.region
>>  ev_list = events.get(region, [])
>>  ev_list.append(event)
>>  events[region] = ev_list
>>return render_to_response(..., {
>>  'events': events, # this might need to
>> # be events.iteritems() or something
>> # like that
>>  ...})
>>
>
> I'm probably doing something wrong in my template now, but that
> version is only returning one event per region, and the order is
> backwards. I tried .order_by('-start_date'), but it didn't appear to
> make any difference.

(fixed top-posting)

Not having your models, I've only been guessing at them which may 
be causing problems.

Before the "return render_to_response(...)" line, you might want 
to try adding one of the following

   assert False, repr(events)
   assert False, repr(events.items())

to see what comes back.

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



Re: There's got to be a better way

2007-07-26 Thread [EMAIL PROTECTED]

I'm probably doing something wrong in my template now, but that
version is only returning one event per region, and the order is
backwards. I tried .order_by('-start_date'), but it didn't appear to
make any difference.


On Jul 26, 3:12 pm, Tim Chase <[EMAIL PROTECTED]> wrote:
> > 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]
>
> Yup...must have been my poor transcription from brain to email.
> Sorry for the confusion.  It's building a list of tuples where
> the first item is the region and the second is the list of events
> in that region.
>
> > What happens if there are no events in one of the regions?
>
> > Does that cause a hit to the DB for each region?
>
> In its current state, yes, it does cause a DB hit for each
> region, data or no data.  Yes, Baxter, filter() is lazy, but it's
> instantiated in the view when called upon, so it is actually
> evaluated even when blank.
>
> To avoid this, an alternative might be something like
>
>future_events = Event.object.filter(
>  start_date__gte=now).select_related(
>  ).order_by('start_date')
>
>events = {} # or SortedDict if order matters
>for event in future_events:  # one DB hit here
>  region = event.club.region
>  ev_list = events.get(region, [])
>  ev_list.append(event)
>  events[region] = ev_list
>return render_to_response(..., {
>  'events': events, # this might need to
> # be events.iteritems() or something
> # like that
>  ...})
>
> This version should only hit the DB once for all the event data
> and also sort the data before it ever gets to you, so you
> (Baxter) can avoid the dictsort in your template.
>
> -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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: There's got to be a better way

2007-07-26 Thread Tim Chase

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

Yup...must have been my poor transcription from brain to email. 
Sorry for the confusion.  It's building a list of tuples where 
the first item is the region and the second is the list of events 
in that region.

> What happens if there are no events in one of the regions?
> 
> Does that cause a hit to the DB for each region?

In its current state, yes, it does cause a DB hit for each 
region, data or no data.  Yes, Baxter, filter() is lazy, but it's 
instantiated in the view when called upon, so it is actually 
evaluated even when blank.

To avoid this, an alternative might be something like

   future_events = Event.object.filter(
 start_date__gte=now).select_related(
 ).order_by('start_date')

   events = {} # or SortedDict if order matters
   for event in future_events:  # one DB hit here
 region = event.club.region
 ev_list = events.get(region, [])
 ev_list.append(event)
 events[region] = ev_list
   return render_to_response(..., {
 'events': events, # this might need to
# be events.iteritems() or something
# like that
 ...})

This version should only hit the DB once for all the event data 
and also sort the data before it ever gets to you, so you 
(Baxter) can avoid the dictsort in your template.

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



Re: There's got to be a better way

2007-07-26 Thread [EMAIL PROTECTED]

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 %}
  {{ region.0 }} region
  
   {% for event in region.1|dictsort:"start_date" %}
 
blah blah blah

   {% endfor %}
  
  {% 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 %}
> >   Happenings in the {{ region.0 }} region
> >   
> >{% for event in region.1 %}
> > {{ event.description }}
> >{% endfor %}
> >   
> >{% 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 django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: There's got to be a better way

2007-07-26 Thread Carl Karsten

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 %}
>   Happenings in the {{ region.0 }} region
>   
>{% for event in region.1 %}
> {{ event.description }}
>{% endfor %}
>   
>{% 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 django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: There's got to be a better way

2007-07-26 Thread Tim Chase

> 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 %}
  Happenings in the {{ region.0 }} region
  
   {% for event in region.1 %}
{{ event.description }}
   {% endfor %}
  
   {% endfor %}

Hope this helps,

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



There's got to be a better way

2007-07-26 Thread [EMAIL PROTECTED]

I'm sure you all will immediately see what I'm doing, and how I could
be doing it better. I'm sure I need to create my own dict or
something, but I'm not sure how I'd do so, or (more importantly), how
I'd work with in the template.

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. It works, but I know I'm being stupid
here and thoroughly violating the DRY principle. Could someone show me
the light?


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