我试图弄清楚我认为很容易找到答案的东西。我在一个项目中使用 django-taggit,我只想在选择标签时返回一个对象列表。我试过这个:
但我无法让它工作。它只是呈现一个空白页面。我认为问题出在我的模板代码中。也许有人可以指出我的方向。任何帮助将不胜感激..非常感谢。
这是我的代码:
模型.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()
视图.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']])
网址.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())
)