Sunday, October 20, 2019

taggit in Django CBV



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 










--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/988e7873-6648-4d04-81a3-a66947501e10%40googlegroups.com.

No comments:

Post a Comment