taggit in Django CBV

56 views
Skip to first unread message

mohamed khaled

unread,
Oct 20, 2019, 7:37:11 PM10/20/19
to Django users


views.py
from django.shortcuts import render, get_object_or_404
from .models import Post
from django.views.generic import ListView
from taggit.models import Tag

def post_list(request, tag_slug=None):
    object_list
= Post.published.all()
    tag
= None


   
if tag_slug:
        tag
= get_object_or_404(Tag, slug=tag_slug)
        object_list
= object_list.filter(tags__name__in=[tag]) # filter the all retrive objects depend on tags

urls.py
 path('', views.post_list, name='post_list'),
 path
('tag/<slug:tag_slug>/',views.post_list, name='post_list_by_tag'),




how can I convert this code into CBV and the results are when I click on any tags in website show me all posts that related to this tag in my DB this code above does that but I want to use CBV instead of FBV 










Om Anirudh

unread,
Oct 21, 2019, 8:49:02 AM10/21/19
to Django users
Reply all
Reply to author
Forward
0 new messages