Skip to content

django taggit

Installation

$ pip install django-taggit

Add taggit to your project’s INSTALLED_APPS setting.

from django.db import models

from taggit.managers import TaggableManager

class Food(models.Model):
    # ... fields here

    tags = TaggableManager()

To display the title of the other suggested blogs as clickable links in your NewsDetailsView, you can modify the template to include links to the suggested blog items. Here's how you can do it:

Tip

from django.db import models
from django.urls import reverse
from taggit.managers import TaggableManager

class NewsItem(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    tags = TaggableManager()
    # Other fields as needed

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('news_detail', kwargs={'slug': self.slug})

    def get_related_blogs(self):
        return NewsItem.objects.filter(tags__in=self.tags.all()).exclude(id=self.id).distinct()[:5]
class NewsDetailsView(DetailView):
    model = NewsItem
    template_name = "news/item.html"
    context_object_name = "news_detail"
    slug_field = "slug"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['related_blogs'] = self.object.get_related_blogs()
        return context
@admin.register(NewsItem)
class NewsItemAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.CharField: { 'widget': CustomCharFieldWidget }
    }
    list_display = ('title', 'content', 'status',  'tag_list', 'publish')

    def get_queryset(self, request):
        return super().get_queryset(request).prefetch_related('tags')

    def tag_list(self, obj):
        return u", ".join(o.name for o in obj.tags.all())
{% extends 'base.html' %}

{% block content %}

<section class="bt-news bt-section-common">
    <div class="container">
        <div class="row">

            <!-- RELATED BLOGS -->
            <div class="col-md-12">
                {% if related_blogs %}
                    <h3>Related Blogs:</h3>
                    <ul>
                        {% for blog in related_blogs %}
                            <li><a href="{{ blog.get_absolute_url }}">{{ blog.title }}</a></li>
                        {% endfor %}
                    </ul>
                {% endif %}
            </div>
        </div>
    </div>
</section>

{% endblock %}

Tags

To include the tags associated with a NewsItem instance in your NewsDetailsView, you can modify the get_context_data method to pass the tags along with other context data. Here's how you can do it:

class NewsDetailsView(DetailView):
    model = NewsItem
    template_name = "news/item.html"
    context_object_name = "news_detail"
    slug_field = "slug"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # Retrieve tags associated with the NewsItem instance
        context['tags'] = self.object.tags.all()
        return context

Now, in your news/item.html template, you can access these tags:

<!-- news/item.html -->
<h1>{{ news_detail.title }}</h1>
<!-- Other details of the news item -->
<ul>
    {% for tag in tags %}
        <li>{{ tag.name }}</li>
    {% endfor %}
</ul>

This will display all the tags associated with the NewsItem instance in your NewsDetailsView. Make sure you have properly associated tags with your NewsItem instances.


Reference