Change - List and Detail View for Data Tags

Created on Oct. 17, 2012, 11:50 p.m. by Hevok & updated on Oct. 17, 2012, 11:55 p.m. by Hevok

To show all the tags in a database a TagList with a generic ListView can be created and for showing the content from an app with a specific tag the queryset of another ListView can be overwritten: ::

# data/urls.py ¶
... ¶
from views import TagList, TaggedList
... ¶
url(r'data/tags/$', Taglist.as_view()), ¶
url(r'data/tag/(?P<tag>\w+)/$', TaggedList.as_view()), ¶
... ¶

# views.py ¶
... ¶
from data.models import Entry ¶
from taggit.models import Tag, TaggedItem ¶
... ¶
class TagList(ListView): ¶
"""Get all the tags in the db.""" ¶
queryset = Tag.objects.all() ¶
template_name = 'data/tag_list.html' ¶
paginate_by = 10 ¶




class TaggedList(ListView): ¶
"""Get all the content for a tag.""" ¶
template_name = "data/tagged_list.html" ¶


def get_queryset(self): ¶
#return TaggedItem.objects.filter(tagiexact=self.kwargs['tag']) # If interested in all tagged models. ¶
return Entry.objects.filter(tags
name__in=[self.kwargs['tag']]) # If only interested in tagged entries. ¶
... ¶

# data/tagged_list.html ¶
... ¶
{% for item in TaggedItem_list %} ¶
{{ item.content_objects }} ¶
{% endfor %} ¶
...

Tags: tagging django

Comment: Added description.

Comment on This Data Unit