Python 如何使category视图使用基于类的视图列出Django中所有具有相关类别的帖子

Python 如何使category视图使用基于类的视图列出Django中所有具有相关类别的帖子,python,django,Python,Django,我想知道如何使用基于类的视图创建类别页面视图我知道如何使用get_object_或_404category在基于函数的视图中创建类别页面视图,slug=None,但我不知道如何在基于类的视图中创建类别页面视图。我试着用谷歌搜索这个,但在类视图中找不到任何与此相关的内容 我知道我可以使用基于函数的视图,但我在整个项目中使用了基于类的视图,所以我想在这里也使用它们 我的代码 models.py url.py 我想在CategoryPostListView中定义get_queryset。但我不确定它是

我想知道如何使用基于类的视图创建类别页面视图我知道如何使用get_object_或_404category在基于函数的视图中创建类别页面视图,slug=None,但我不知道如何在基于类的视图中创建类别页面视图。我试着用谷歌搜索这个,但在类视图中找不到任何与此相关的内容

我知道我可以使用基于函数的视图,但我在整个项目中使用了基于类的视图,所以我想在这里也使用它们

我的代码

models.py

url.py

我想在CategoryPostListView中定义get_queryset。但我不确定它是否有效。

是的。您可以在基于类的视图中使用get_object_或_404。只需将此添加到您的CategoryPostListView:


有关更多信息,您可以在django官方网站上阅读。首先,如果您使用ListView并希望显示帖子列表,则需要model=Post

接下来,您可以在get_queryset方法中调用get_object_或_404。您可以使用'self.kwargs['slug']从URL访问slug

最后,您可以过滤queryset以仅返回该类别中的帖子

class CategoryPostListView(LoginRequiredMixin, ListView):
    model = Post
    template_name = 'posts/category_detail.html'

    def get_queryset(self):
       category = get_object_or_404(Category, slug=self.kwargs['slug'])
       return super(CategoryPostListView, self).get_queryset().filter(category=category)

请注意,您的问题与文档中的问题非常相似。

请显示您的视图。py以及您在其中尝试的内容。您阅读了示例吗?@BearBrown坦白地说,我没有阅读所有内容,但没有完成。@shotgunner我已更新了我的问题。但它没有呈现与类别相关的帖子。该字段的名称是什么要查询的类别?我给它取了个名字。把它改成它应该是什么我想它是你数据库中的slug我为你改的它不是呈现所有与类别相关的帖子它只是呈现类别列表名称我想制作一个类别页面,呈现所有与类别相关的帖子category@adityakumar是的,这是我的错,我错误地命名了获取查询集的方法,它必须是获取查询您需要覆盖获取查询集,而不是获取查询集。
from django.urls import path
from django.urls import path, include
from .views import PostView, PostDetailView,LatestPostView, CategoryPostListView

urlpatterns = [
    path('', PostView.as_view(), name='posts-home'),
    path('latest/', LatestPostView.as_view(), name='posts-latest'),
    path('<slug>', PostDetailView.as_view(), name='posts-detail'),
    path('category/<slug>', CategoryPostListView.as_view(), name='category-detail'),

]
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin

from django.shortcuts import redirect, render,get_object_or_404

#class based view
from django.views.generic import ListView, DetailView
from .models import Post, Category

class PostView(ListView):
   template_name = 'posts/home.html'
   model = Category
   context_object_name = 'all_categs'

   def get_queryset(self):
      if self.request.user.is_authenticated:        
         return Category.objects.all()
      else:
         return Category.objects.all().exclude(title__iexact = 'Featured')[:6]

   def get_context_data(self):
      if not self.request.user.is_authenticated:   
         fcategory = Category.objects.get(title__iexact = 'Featured')        
         context = super(PostView, self).get_context_data()
         context['latest_posts'] = Post.objects.exclude(category= fcategory).order_by('-date_posted')[0:6]
         context['featured_posts'] = Post.objects.all().filter(category= fcategory).order_by('-date_posted')[0:6]
         return context
      else:
         fcategory = Category.objects.get(title__iexact = 'Featured') 
         context = super(PostView, self).get_context_data()
         context['latest_posts'] = Post.objects.order_by('-date_posted')
         context['featured_posts'] = Post.objects.all().filter(category= fcategory).order_by('-date_posted')[0:6]
         return context

   # def get_success_url(self):
   #     return reverse('home') #add your path


class LatestPostView(LoginRequiredMixin, ListView):
   template_name = 'posts/post_latest.html'
   model = Post
   context_object_name = 'Posts'
   ordering = ['-date_posted']
   paginate_by = 6


class PostDetailView(LoginRequiredMixin,DetailView):
    model = Post
    template_name = 'posts/post_detail.html'



class CategoryPostListView(LoginRequiredMixin, ListView):
   model = Category
   template_name = 'posts/category_detail.html'

   # def get_queryset(self):
   #    category = get_object_or_404(Category, )
class CategoryPostListView(LoginRequiredMixin, ListView):
    model = Post
    template_name = 'posts/category_detail.html'

    def get_queryset(self):
        category = get_object_or_404(Category, slug=self.kwargs['slug'])
        # do another stuffs here
        return Post.objects.filter(category=category)
class CategoryPostListView(LoginRequiredMixin, ListView):
    model = Post
    template_name = 'posts/category_detail.html'

    def get_queryset(self):
       category = get_object_or_404(Category, slug=self.kwargs['slug'])
       return super(CategoryPostListView, self).get_queryset().filter(category=category)