我正在尝试使用这个模型过滤博客文章:
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
)时,我收到了所有的帖子,过滤不起作用。有什么问题吗?
1条答案
按热度按时间h79rfbju1#
您缺少“覆盖”
posts
变量。