在Django中实现帖子筛选页面

huangapple go评论60阅读模式
英文:

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=&quot;amount of likes&quot;)
    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(&#39;type&#39;)
    order = request.GET.get(&#39;order&#39;)
    author = request.GET.get(&#39;author&#39;)
    search = request.GET.get(&#39;search&#39;)

    if slug:
        cat = get_object_or_404(Category , slug=slug)
        posts.filter(category = cat)

    if search != &#39;&#39; and search is not None:
        posts.filter(Q(title__icontains = search))

    if kind != &#39;&#39; and kind is not None:
        if kind == &#39;podcast&#39;:
            posts.filter(is_podcast=True)
        if kind == &#39;all&#39;:
            pass
        if kind == &#39;post&#39;:
            posts.filter(is_podcast=False)  

    con = {&#39;posts&#39; : posts}
    return render(request , &#39;Weblog/posts.html&#39; , con)

Here is the urls.py too:

path(&#39;posts/&#39;, Index_view),
re_path(r&#39;^posts/(?P&lt;slug&gt;[\w-]+)/$&#39;, 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':  --&gt; 无用
        #    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(&#39;type&#39;)
    order = request.GET.get(&#39;order&#39;)
    author = request.GET.get(&#39;author&#39;)
    search = request.GET.get(&#39;search&#39;)

    if slug:
        cat = get_object_or_404(Category , slug=slug)
        posts = posts.filter(category = cat)

    if search != &#39;&#39; and search is not None:
        posts = posts.filter(Q(title__icontains = search))

    if kind != &#39;&#39; and kind is not None:
        if kind == &#39;podcast&#39;:
            posts = posts.filter(is_podcast=True)
        #if kind == &#39;all&#39;:  --&gt; useless
        #    pass
        if kind == &#39;post&#39;:
            posts = posts.filter(is_podcast=False)  

    con = {&#39;posts&#39; : posts}
    return render(request , &#39;Weblog/posts.html&#39; , con)

huangapple
  • 本文由 发表于 2023年6月16日 13:13:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定