On May 10, 5:03 pm, Jamie Pittock <ja...@erskinedesign.com> wrote:
> Please excuse the subject if it makes no sense.
>
> Simplified, I have two models Pub and Town.  Pub has a ForeignKey
> field called 'town'.  I'm using the generic object_list view to
> display all pubs in a particular town (based on a town slug in the
> url).  Something like this...
>
> def pubs_by_town(request, town_slug):
>     town = get_object_or_404(Town, slug=town_slug)
>     return list_detail.object_list(
>         request,
>         queryset=Pub.objects.filter(town__slug=town_slug),
>         extra_context={ 'town': town },
>         template_name='pubs/pubs_by_town.html')
>
> <h1>Pubs in {{ town.name }}</h1>
>
> {% for pub in object_list %}
> * {{ pub.name }}
> {% endfor %}
>
> This all works fine but getting the town object to use in the h1 seems
> a bit redundant seeing as every pub instance in object_list will have
> the same name of the town anyway.
>
> Is it possible to use the queryset to pass the town name as
> extra_context so that a variable is available to use in my template?
> Is there a better way?

If you really wanted to avoid the extra query, you could do
{{ object_list.0.town.name }}
but I'm not sure if the lack of clarity is worth it.
--
DR.
--~--~---------~--~----~------------~-------~--~----~
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