Django Taggit

If you want to add tags to your blog posts or articles, then this library is suitable for you. It is a reusable Django application designed to make adding tagging to your project easy and fun.

Note: django-taggit works with Django 1.11+ and Python 2.7 / 3.4+

Installation

pip install django-taggit

Add "taggit" to your project’s INSTALLED_APPS setting.

Now run: 

python manage.py migrate

Usage

from django.db import models 
from taggit.managers import TaggableManager 
class Blog(models.Model): 
    # ... fields here 
    tags = TaggableManager()

 

How to add tags to your post or article from django admin

  • In your django admin, there should be a model named tags

  • Click Add custom tags, put the tag name and click on save.

 

  • By now, you should have list of tags created; something like this.

  • Now when you add your blog post or article, in your tags field type the name of tags you created created separated by comma(,)

That's it for admin.

Other way of creating and adding tags to post it given below:

post = Blog.objects.create(name="how to use taggit")
post.tags.add("python", "django", "ubuntu")
post.tags.all()
[<Tag: python>, <Tag: django>, <Tag: ubuntu>]
post.tags.remove("ubuntu")
post.tags.all()
[<Tag: python>, <Tag: django>]
Blog.objects.filter(tags__name__in=["python"])
[<Blog: post1>, <Blog: post2>]