返回所选标签中的对象列表

时间:2014-05-08 17:50:49

标签: python django django-taggit

我正在试图找出我认为很容易找到答案的内容。我在项目中使用django-taggit,我只想在选择标签时返回对象列表。我试过这个:

How do I create list and detail views for django-taggit?

但我无法让它发挥作用。它只是呈现一个空白页面。我认为问题出在我的模板代码中。也许有人可以指出我的方向。任何帮助将不胜感激。谢谢。

这是我的代码:

models.py

from taggit.managers import TaggableManager
from django.template.defaultfilters import slugify
from ckeditor.fields import RichTextField 
from taggit.models import TaggedItemBase

class Tagged(TaggedItemBase):
content_object = models.ForeignKey('Shows')

class Shows(models.Model):

title = models.CharField(max_length=40)
slug = models.SlugField(null=True, blank=True, unique=True)
tags = TaggableManager(through=Tagged)
hosts = models.ManyToManyField('Host', blank=True, null=True)
featured = models.BooleanField(default=False)
thumbnail = FilerImageField(related_name="thumbnail", help_text="Image should be: 550 X 350.")
playing_next = models.DateTimeField(null=True, blank=True)
description = RichTextField()

views.py:

class TaggedList(ListView):
queryset = Shows.objects.all()
paginate_by = 10
template_name = "tagged.html"

def get_queryset(self):
    return Shows.objects.filter(tags__name__in=[self.kwargs['tag']])

urls.py:

urlpatterns = patterns('radio.views',
                   url(r'^$', 'main', name='app_main'),
                   url(r'^(?P<slug>[^\.]+)/detail/$', 'detail_view', name='detailsview'),
                   url(r'^(?P<tag>\w+)/$', TaggedList.as_view()),
                   url(r'^tagged/(?P<tag>\w+)/$', TaggedList.as_view())

                   )

2 个答案:

答案 0 :(得分:1)

模板代码:

{% for objects in object_list %}
    {{ objects.title }}
    {{ objects.tag }}
    {{ objects.slug }} 
     ------
{% endfor %}

答案 1 :(得分:0)

你是否在模板中迭代了object_list?因为LiseView中的默认列表名称为object_list:doc:https://docs.djangoproject.com/en/dev/ref/class-based-views/generic-display/#listview