英文:
Implementation of the post filter page in django
问题
我正在尝试使用这个模型来过滤博客文章:
class Post(models.Model):
    is_podcast = models.BooleanField(default=False)
    category = models.ManyToManyField(Category)
    title = models.CharField(max_length=500)
    slug = models.SlugField(allow_unicode=True, unique=True, null=True, blank=True)
    body = RichTextUploadingField()
    likes_count = models.IntegerField(default=0, help_text="amount of likes")
    vip_members_only = models.BooleanField(default=False)
    def __str__(self):
        return self.title
我正在使用这个视图来过滤帖子:
def Index_view(request, slug=None):
    posts = Post.objects.filter()
    kind = request.GET.get('type')
    order = request.GET.get('order')
    author = request.GET.get('author')
    search = request.GET.get('search')
    if slug:
        cat = get_object_or_404(Category, slug=slug)
        posts.filter(category=cat)
    if search != '' and search is not None:
        posts.filter(Q(title__icontains=search))
    if kind != '' and kind is not None:
        if kind == 'podcast':
            posts.filter(is_podcast=True)
        if kind == 'all':
            pass
        if kind == 'post':
            posts.filter(is_podcast=False)
    con = {'posts': posts}
    return render(request, 'Weblog/posts.html', con)
这是 urls.py:
path('posts/', Index_view),
re_path(r'^posts/(?P<slug>[\w-]+)/$', Index_view),
当我使用这个URL(http://127.0.0.1:8000/blog/posts/some-slug/?search=test)时,我收到所有的帖子,过滤不起作用。问题是什么?
【注意】:你提供的URL中的路径是/blog/posts/而不是/posts/。因此,在urls.py中应该将路径更改为path('blog/posts/', Index_view)。另外,你应该在过滤后更新posts变量,因为filter()方法返回一个新的查询集,而不会修改原始查询集。可以使用posts = posts.filter(...)来更新它。
英文:
I am trying to filter blog posts using this model:
class Post(models.Model):
    is_podcast = models.BooleanField(default=False)
    category = models.ManyToManyField(Category)
    title = models.CharField(max_length=500)
    slug = models.SlugField(allow_unicode=True , unique=True , null=True , blank=True)
    body = RichTextUploadingField()
    likes_count = models.IntegerField(default=0 , help_text="amount of likes")
    vip_members_only = models.BooleanField(default=False)
    def __str__(self):
        return self.title
And I am using this view to filter the posts:
def Index_view(request , slug=None):
    posts = Post.objects.filter()
    kind = request.GET.get('type')
    order = request.GET.get('order')
    author = request.GET.get('author')
    search = request.GET.get('search')
    if slug:
        cat = get_object_or_404(Category , slug=slug)
        posts.filter(category = cat)
    if search != '' and search is not None:
        posts.filter(Q(title__icontains = search))
    if kind != '' and kind is not None:
        if kind == 'podcast':
            posts.filter(is_podcast=True)
        if kind == 'all':
            pass
        if kind == 'post':
            posts.filter(is_podcast=False)  
    con = {'posts' : posts}
    return render(request , 'Weblog/posts.html' , con)
Here is the urls.py too:
path('posts/', Index_view),
re_path(r'^posts/(?P<slug>[\w-]+)/$', Index_view),
When I use this URL (http://127.0.0.1:8000/blog/posts/some-slug/?search=test), I receive all the posts and the filtering does not work. What is the problem?
答案1
得分: 1
你忘记了“覆盖”posts变量。
def Index_view(request , slug=None):
    posts = Post.objects.all()
    kind = request.GET.get('type')
    order = request.GET.get('order')
    author = request.GET.get('author')
    search = request.GET.get('search')
    if slug:
        cat = get_object_or_404(Category , slug=slug)
        posts = posts.filter(category = cat)
    if search != '' and search is not None:
        posts = posts.filter(Q(title__icontains = search))
    if kind != '' and kind is not None:
        if kind == 'podcast':
            posts = posts.filter(is_podcast=True)
        #if kind == 'all':  --> 无用
        #    pass
        if kind == 'post':
            posts = posts.filter(is_podcast=False)  
    con = {'posts' : posts}
    return render(request , 'Weblog/posts.html' , con)
英文:
You are missing to "overwrite" the posts variable.
def Index_view(request , slug=None):
    posts = Post.objects.all()
    kind = request.GET.get('type')
    order = request.GET.get('order')
    author = request.GET.get('author')
    search = request.GET.get('search')
    if slug:
        cat = get_object_or_404(Category , slug=slug)
        posts = posts.filter(category = cat)
    if search != '' and search is not None:
        posts = posts.filter(Q(title__icontains = search))
    if kind != '' and kind is not None:
        if kind == 'podcast':
            posts = posts.filter(is_podcast=True)
        #if kind == 'all':  --> useless
        #    pass
        if kind == 'post':
            posts = posts.filter(is_podcast=False)  
    con = {'posts' : posts}
    return render(request , 'Weblog/posts.html' , con)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论