Im使用django-taggit爲博客創建標籤系統。如何分隔和過濾對象,以便只顯示具有選定標記的對象?有點像StackOverflow如果你點擊django 創建標籤索引Django
它會給你所有的問題標籤django。我嘗試了this blog post上描述的方法,但我得到了IndexError: tuple index out of range
。這是我正在使用的代碼:
url(r'^tagged/(?P<tag>[\w-]+)/$', TagView.as_view(), name='tag_url'),
class TagView(ListView):
context_object_name = 'blog'
template_name = 'links/index.html'
def get_queryset(self):
return Blog.objects.filter(tags__name__in=[self.args[0]])
def get_context_data(self, **kwargs):
context = super(TagView, self).get_context_data(**kwargs)
context['requested_tag'] = self.args[0]
return context
<a href='{% url tag_url tag=tag %}'>{{ tag.name }}</a>
我是否缺少一些讓這種方法工作?
看起來這是一個很常見的編程必要性。也許你知道更好的方法...感謝您的想法!
編輯:根據@凱瑟琳的建議TagView:
class TagView(ListView):
model = Blog
context_object_name = 'blog_list'
template_name = 'tag-list.html'
def get_queryset(self):
queryset = super(TagView, self).get_queryset()
return queryset.filter(tags__name__in=self.kwargs['tags'])
class Blog(models.Model):
name = models.CharField(max_length=50)
date = models.DateTimeField()
slug = models.SlugField()
article = models.TextField()
tags = TaggableManager()
def __unicode__(self):
return self.name
標籤list.html:
{% block content %}
stuff
{% for blog in blog_list %}
{{ blog.article }}
{{ blog.name }}
{% endfor %}
{% endblock %}
的blog_list沒有模板存在,沒有博客的對象是可用。相反,只有'東西'呈現給模板。任何想法表示讚賞!謝謝!
指南:http://jquery.webspirited.com/2011/02/jquery-tagit -a-jquery-tagging-plugin/ – catherine 2013-02-27 03:24:10
對不起,但那不是我所需要的。我想弄清楚如何配置我的URL和視圖來創建標籤的歸檔索引。因此,如果用戶點擊一個標籤,對於ex'test',它將鏈接到所有具有相同標籤的對象集合:'example.com/tagged/test /'。就像在StackOverflow上單擊標籤'django'一樣,它將您鏈接到標記爲django:'stackoverflow.com/questions/tagged/django /'的問題集合。但是,感謝您的意見。 – 2013-02-27 04:13:29
啊,正在搜索... – catherine 2013-02-27 04:15:06