List and Detail View for Data Tags

Created on Oct. 17, 2012, 11:50 p.m. by Hevok & updated by Hevok on May 2, 2013, 5:24 p.m.

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/
from views import TagList, TaggedList
url(r'data/tags/$', Taglist.as_view()),
url(r'data/tag/(?P<tag>\w+)/$', TaggedList.as_view()),

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(tag__iexact=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
Categories: Tutorial
Parent: Web Framework

Update entry (Admin) | See changes

Comment on This Data Unit