创建标签索引Django [英] Creating Tag Index Django
问题描述
我使用所描述的方法,但是我得到一个 IndexError:元组索引超出范围
。这是我使用的代码:
url(r'^ tagged /(?P< tag> [\w- ] +)/ $',TagView.as_view(),name ='tag_url'),
class TagView(ListView):
context_object_name ='blog'
template_name =' link / 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)
上下文['requested_tag'] = self.args [0]
返回上下文
< a href ='{%url tag_url tag = tag%}'> {{tag.name}}< / a>
我错过了一些让这种方法工作的东西吗?
看起来这是一个很常见的编程需要。也许你知道一个更好的方法...感谢您的想法!
编辑:基于@ catherine建议的TagView: / p>
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 [标签'])
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
tag-list.html:
{%block content%}
stuff
{%blog_list%中的博客}
{{blog.article}}
{{blog.name}}
{%endfor%}
{%endblock%}
该blog_list不存在于模板中,没有可用的博客对象。相反,只有东西被呈现给模板。任何想法都赞赏!谢谢!
这个答案是基于编辑:基于@ catherine的建议的TagView:
你有一个打字错误,在 get_queryset
方法中:
code> return queryset.filter(tags__name__in = self.kwargs ['tags'])
您使用标签
而不是标签
,因此应该是:
return queryset.filter(tags__name__in = [self.kwargs ['tag']])
Im using django-taggit to create a tagging system for a blog. How do you separate and filter objects so that only ones with selected tags are shown? Kind of like how on StackOverflow if you click on django
it will give you all the questions tagged django. I have tried the method described on this blog post, but I get an IndexError: tuple index out of range
. This is the code I am using:
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>
Am I missing something to get this method to work?
It seems like this is a pretty common programming necessity. Maybe you know a better method... Thanks for your ideas!
EDIT: TagView based on @catherine's suggestions:
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
tag-list.html:
{% block content %}
stuff
{% for blog in blog_list %}
{{ blog.article }}
{{ blog.name }}
{% endfor %}
{% endblock %}
The blog_list does not exist in the template, and no blog objects are available. Rather, only 'stuff' is rendered to the template. Any ideas are appreciated! Thanks!
This answer is based on "EDIT: TagView based on @catherine's suggestions:".
You have a typo, in get_queryset
method:
return queryset.filter(tags__name__in=self.kwargs['tags'])
you use tag
and not tags
thus it should be:
return queryset.filter(tags__name__in=[self.kwargs['tag']])
这篇关于创建标签索引Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!