文章列表页看不到已发表的文章

来源:7-4 完成文章列表页开发

weixin_慕仙3585306

2023-08-06

可以发布文章,也有发布成功的提示,再尝试创建同样标题的文章也会正确地提示标题已存在,但无论是文章页还是草稿页,见不到一篇文章,只有“快来成为第一个写文章的吧!”

代码如下

# views.py

import ...

class ArticleListView(LoginRequiredMixin, ListView):
    """已发布的文章的列表"""

    model = Article
    paginate_by = 10
    context_object_name = 'articles'
    template_name = 'articles/article_list.html'

    def get_context_data(self, *args, **kwargs):
        context = super(ArticleListView, self).get_context_data(*args, **kwargs)
        context['popular_tags'] = Article.objects.get_counted_tags()
        return context

    queryset = Article.objects.get_published()


class DraftListView(ArticleListView):
    '''草稿箱文章列表'''

    def get_queryset(self) -> QuerySet[Any]:
        return Article.objects.filter(user=self.request.user).get_drafts()


class ArticleCreateView(LoginRequiredMixin, CreateView):
    '''发表文章'''

    model = Article
    form_class = ArticleForm
    template_name = 'articles/article_create.html'
    message = '文章创建成功'

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(ArticleCreateView, self).form_valid(form)
    
    def get_success_url(self) -> str:
        messages.success(self.request, self.message)
        return reverse_lazy("articles:list")
# models.py

import ...

class ArticleQuerySet(models.query.QuerySet):

    def get_published(self):
        return self.filter(status='P')
    
    def get_drafts(self):
        return self.filter(status='D')
    
    def get_counted_tags(self):
        '''统计所有已发表的文章中的标签中标签数>0的标签的标记数'''
        tag_dict = {}
        for obj in self.get_published():
            for tag in obj.tags.names():
                if tag not in tag_dict:
                    tag_dict[tag] = 1
                else:
                    tag_dict[tag] += 1
        return tag_dict.items()


class Article(models.Model):
    
    STATUS = (('D', 'Draft'), ('P', "Published"))

    id = models.BigAutoField(primary_key=True)
    title = models.CharField(max_length=255, null=False, unique=True, verbose_name='标题')
    user = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL,
                             related_name='author',verbose_name='作者')
    image = models.ImageField(upload_to="articles_pictures/%Y/%m/%d", verbose_name='文章图片')
    slug = models.SlugField(max_length=255, verbose_name='(URL)别名')
    status = models.CharField(max_length=1, choices=STATUS, default='D', verbose_name='状态')
    content = models.TextField(verbose_name='内容')
    editable = models.BooleanField(default=False, verbose_name='可编辑')
    tags = TaggableManager(verbose_name='标签')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    objects = ArticleQuerySet.as_manager()

    class Meta:
        verbose_name = '文章'
        verbose_name_plural = verbose_name
        ordering = ('created_at',)

    def __str__(self):
        return self.title
写回答

1回答

weixin_慕仙3585306

提问者

2023-08-06

破案了,7.7里讲到了这个bug,是状态没设置。我这边草稿的页面有个写法不对,导致发表的和草稿都看不到

0
1
Jack
好的👌
2023-08-06
共1条回复

Django高级实战 开发企业级问答网站

融合Django高级用法/算法/设计模式/TestCase测试/云计算打造项目

900 学习 · 756 问题

查看课程