django-taggit
is a Django application/package used to add tags to blogs, articles etc. With this package it is really easy to add the tags and their functionality to our django project.
First we have to install the package:
pip install django-taggit
Little note: For my Django project I used the version 6.0.1 of django-taggit
.
Add taggit
in settings.py to INSTALLED_APPS
:
# blog_project/settings.py
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
# custom apps
"account",
"blog",
"core",
# packages/libraries:
"taggit",
]
After we add the django-taggit
to our model:
# blog/models.py
from django.db import models
from django.urls import reverse
from django.utils import timezone
from taggit.managers import TaggableManager
class Post(models.Model):
class Status(models.TextChoices):
DRAFT = "DF", "Draft"
PUBLISHED = "PB", "Published"
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date="publish")
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=2, choices=Status, default=Status.DRAFT)
author = models.ForeignKey(
"account.User", on_delete=models.CASCADE, related_name="blog_posts"
)
tags = TaggableManager()
tags = TaggableManager()
: You add the TaggableManager()
to the model you want to add tags to.When the TaggableManager
is added to the model, you have to migrate:
python manage.py makemigrations
python manage.py migrate
makemigrations
: This command detects changes to your model, such as adding the TaggableManager()
field, and creates migration files. These files describe the database changes that are required, like creating new tables or fields.migrate
: This command applies the changes described in the migration files to the database, updating it to reflect the current state of your models.Now, your setup is done, you can start creating tags in your shell
. Open your python shell with the command:
python manage.py shell
Personally, I use the package ipython
for my python shell.
In [1]: from blog.models import Post
In [2]: post = Post.objects.first()
In [3]: post
Out[3]: <Post: How to get the verbose_name of an attribute of a model>
In [4]: post.tags.add('django', 'django-model')
In [5]: post.tags.all()
Out[5]: <QuerySet [<Tag: django>, <Tag: django-model>]>
Just like this you can add tags to your model Post
.
After creating for one Post
instance these tags, we can display the tags in our listing template.
<!-- blog/templates/post/list.html -->
{% extends "base.html" %}
{% block title %} Django Blog {% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
{% if post.tags.exists %}
<p class="tags">Tags: {{ post.tags.all|join:", " }}</p>
{% endif %}
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% include "pagination.html" with page=page_obj %}
{% endblock %}
{% if post.tags.exists %}
: Later, all our Post
instances will have tags, but currently only one Post
instance has tags, so I check if the `Post' instance has tags.{{ |join:", " }}
: The template filter |join
is like the python join()
method. It displays the tags separated by comma.