Adding Tags Using Django-Taggit in Django Project

Django-Taggit is a Django application which is used to add tags to blogs, articles etc. It makes very easy for us to make adding the tags functionality to our django project.

Setting up Django Project

  • Installing django-taggit

    pip install django-taggit
  • ADD it to Main Project’s settings.py file

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'django.contrib.humanize',
        'taggit'
    ]

    chevron_right

    
    

  • Changes To models.py file



    Add TaggableManager in your post, blog, article modal

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    from django.db import models
    from django.utils.timezone import now 
    from taggit.managers import TaggableManager
      
    class Post(models.Model):
        postid = models.AutoField(primary_key=True)
        title = models.CharField(max_length=255)
        content = models.TextField()
        author = models.CharField(max_length=100
        postauthor = models.ForeignKey(User, on_delete=models.CASCADE)
        slug = models.SlugField(unique=True)
        category = models.CharField(max_length=200
        timestamp = models.DateTimeField(default=now, blank=True)
        tags = TaggableManager()

    chevron_right

    
    

  • After adding TaggableManager run following commands in terminal

    python manage.py makemigrations
    python manage.py migrate
    
  • The TaggableManager will show up automatically as a field in post modal 

    The Taggit Modal will also appear automatically as a seaprate modal in admin panel 

    Taggit Modal In Admin Panel

Adding tags 

1) If the tags input doesn’t contain any commas or double quotes, it will be taken as space-delimited list of
tag names.

geeks for geeks => "geeks", "for", "geeks"

2) If the tags input does contain commas or double qoutes then :

       
a) geeks, geeks for geeks            =>   "geeks", "geeks for geeks" 
b) "geeks, forgeeks" geek geeksfor   =>   "geeks, forgeeks", "geek", "geeksfor"
c) "geeks, forgeeks", geek geeksfor  =>   "geeks, forgeeks", "geeks geeksfor"
d) geeks "for" geeks                 =>   "geeks", "for", "geeks"

After Adding Tags to the posts the taggit modal will look like :

All the tags which are associated with different posts will appear in tags modal under taggit

On clicking the particular tag, There will come a list of all the posts associated with that particular tag. Now this model can be used with the project like other models

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.