Get all tags from taggit
Asked Answered
P

3

13

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()
Pendergrass answered 15/10, 2012 at 10:56 Comment(0)
W
26

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.

Woodland answered 15/10, 2012 at 11:3 Comment(8)
Thanks. Should I add "tags=Tag.objects.all()" under Post? And how to display it?Pendergrass
Do you mean for one post i.e. post = Post.objects.get(pk=1)? Or all posts (Post.objects.all())?Lewendal
@Vino No, you shouldn't add it to your Post model. Edit the current view where you want to display the sidebar and pass the tag list to your template. Tag.objects.all() is the standard way to retrieve objects through the Django ORM. Have a look to the documentation for more details. If you need only the tag names, use a list comprehension: tag_names = [tag.name for tag in Tag.objects.all()]Woodland
@Skidoosh Not for one post. I need all the tags in the blog.Pendergrass
@PaoloMoretti Thanks. Right now I am using only generic views. I have not created any View. 'views.py' is empty. So should I create a new view? (I am a newbie).Pendergrass
@Vino No, you don't have to create a new view. You can follow these instructions to display a tag list in any template you like.Woodland
Great! Done! I followed the instructions - installed django-taggit-templatetags and used it to display the tags! Thanks a lot @PaoloMoretti et al. I love this django community in stackoverflow :)Pendergrass
@PaoloMoretti I will also learn the other standard way soon (using views). Thanks for sharing :)Pendergrass
K
5

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.

Karttikeya answered 12/9, 2014 at 16:16 Comment(0)
C
2

I know this is an old question...but I'm a Django newbie and found this question while looking for a way to fill an Ajax dropdown with all tag options. I figured out a way with djangorestframework and wanted to put a more complete solution here for others (OP would also be able to populate a sidebar with the response, or do anything else with it).

This adds an api endpoint tag so you can not only view them by navigating to /tag/ but get a JSON response suitable for Ajax (ergo, this assumes you have djangorestframework installed and in use).

serlializers.py

from taggit.models import Tag
class MyTagSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tag
        fields = ['name', 'slug']

views.py

from taggit.models import Tag
class TagViewSet(viewsets.ModelViewSet):
    """
    Not using taggit_serializer.serializers.TaggitSerializer because that's for listing
    tags for an instance of a model
    """
    queryset = Tag.objects.all().order_by('name')
    serializer_class = MyTagSerializer

urls.py

router.register(r'tag', TagViewSet)

And, if you need the ajax:

$(document).ready(function(){

   $.ajax({
      url : "/tag/",
      dataType: "json",
      type: 'GET'
    }).done(function(response){
      for (var i in response) {
        tagEntry = response[i];
        $('#tagdropdown').append($('<option>', {
            value: tagEntry.name,
            text: tagEntry.name
        }));
      }
    }).fail(function(response){
      console.log('failed!');
      console.log(response);
    });

});
Cheltenham answered 16/5, 2020 at 1:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.