Hi, Tom.  I'm not sure I always give the very best advise as to how to
do something, but I give you the easiest way I know of.  So, that
being the case, I'd use the object_detail generic view instead of
object_list.  Object_list doesn't accept a slug or object_id, whereas
object_detail does.  Just look at it as though you're viewing the
detail of a specific tag, which may happen to be a list of posts.

Anyhow, try changing your dict to:

tag_view = {
    'queryset': Tag.objects.all(),
    'slug_field': 'slug',
    'template_object_name': 'current_tag', # purely optional
}

A couple of notes about the database API.  First of all, post_set is
only going to be accessible through a particular instance of a Tag and
not the Tag model.  For example, this works:

my_tag = Tag.objects.get(pk=1)
my_tag.post_set.all() # gives you a queryset of all posts associated
with the tag

In the dict above, I used Tag.objects.all(), which passes a queryset
of all of the Tag objects to the generic view, which then does the
selecting for you based on your slug field.

Okay, moving on.  Now, your view should provide your template with a
variable called 'current_tag'.  Not to complicate things, but if you
left out the third line above (template_object_name), your template
variable would simply be 'object'.  They both have the same result,
but one makes your templates easier to understand.

Now, an extremely basic template could look like this (templates/
app_name/tag_detail.html):

<html>
<body>
{% for post in current_tag.post_set.all %}
  <h2>{{ post.title }}</h2>
  <p>{{ post.body }}</p>
{% endfor %}
</body>
</html>

Note that current_tag is the variable that contains the instance of
your tag, which is chosen based on the slug in the url.  You can now
cycle through all of the posts using current_tag.post_set.all

Hope that helps!

Branton


On Jun 2, 4:05 pm, Tom Baylis <[EMAIL PROTECTED]> wrote:
> Hi
>
> I'm fairly new to django and I'm having problems with the generic views.
> As many others I'm making a blog application, and I want to use the
> object_list view to construct a list of all Posts with a certain Tag (My
> code is further down). I've been looking through the examples and the
> djangobook for this and tried to find ehlp with google. The problem is
> that I can't find an understandable explanation on how to do this. How
> do I contruct the queryset to return a correct list with Posts with the
> certain tag?
>
> code:
>
> urlpatterns = patterns('django.views.generic.list_detail',
>     (r'^tag/(?P<slug>[-\w]+)/', 'object_list', tag_view),
> )
>
> tag_view = {
>     'queryset': Tag.post_set.filter(),
>
> }
>
> class Tag(models.Model):
>     slug = models.SlugField('Slug')
>     title = models.CharField('Title', maxlength=30)
>     description = models.TextField('Description')
>
> class Post(models.Model):
>     slug = models.SlugField('Slug')
>     assoc_tags = models.ManyToManyField(Tag)
>     title = models.CharField('Title', maxlength=30)
>     date = models.DateTimeField('Date')
>     body = models.TextField('Body Text')
>
> I'd be thankful for any replies or links to examples. Have a nice
> weekend while you're at it!
>
> / Tom Baylis


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

Reply via email to