Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all tags from taggit

How to get all the (unique) tags from django-taggit? I would like to display all the tags in a side bar. Currently I am able to get all the tags for a particular post, but now I need to get all the unique tags in the entire blog.

code in models.py:

from django.db import models
from taggit.managers import TaggableManager

# Create your models here.
class Post(models.Model):
    title = models.CharField(max_length=100)
    body = models.TextField()
    created = models.DateTimeField()
    tags = TaggableManager()
like image 471
Robby Avatar asked Oct 15 '12 10:10

Robby


2 Answers

You can use all() to get all the tags in your database:

from taggit.models import Tag
tags = Tag.objects.all()

If you need a complete solution, have a look at django-taggit-templatetags. It provides several templatetags, including one for tag list, to expose various taggit APIs directly to templates.

like image 164
Paolo Moretti Avatar answered Nov 04 '22 23:11

Paolo Moretti


The currently maintained fork supporting newer versions of django is: https://github.com/fizista/django-taggit-templatetags2

django-taggit-templatetags has not been maintained for some years.

like image 31
Risadinha Avatar answered Nov 04 '22 22:11

Risadinha