Get all tags from taggit
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.
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.
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);
});
});