Django 框架:视图设置问题

Django Framework : Problems with views settings

我正在学习 Django 框架并遇到一些我不明白的问题。
事实上,我安装了像 Polls/ Blog/ 和我的主页/ 这样的应用程序,并按我的意愿运行。

我的问题是将每个数据显示到我的 homepage_index.html 中,例如:
_ 我的投票包含多少问题
_ 我的博客包含多少篇文章

这两个信息来自不同的应用程序。

我从 Django 教程中找到了一个解决方案,可以像这样通过示例显示我的最后 3 个 Question.objects。

homepage/views.py :

from django.views import generic
from django.utils import timezone
from polls.models import Question


class homepage(generic.ListView):
    template_name = 'homepage/homepage_index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        return Question.objects.filter(pub_date__lte=timezone.now()).order_by('-pub_date')[:3]

我的homepage_index.html工作:

 {% if latest_question_list %}
     <ul>
        {% for question in latest_question_list %}
            <li><a style="color: white; text-decoration: none;" href="{% url 'polls:detail.html' question.id %}">{{ question.question_text }}</a></li>
        {% endfor %}
     </ul>
{% else %}
     <p>No polls are available.</p>
{% endif %}

我如何在 homepage/views.py 中定义多个 context_object_name 和多个查询集?
我试着阅读了许多文档,但我仍然失败

您可以在官方文档中找到更多详细信息,您需要做的与演示中的 now 值相同。 https://docs.djangoproject.com/en/4.0/ref/class-based-views/generic-display/#listview

您需要覆盖 get_context_data

  1. 计算问题或博客或其他数据的数量,将它们放入上下文中
  2. 然后在 homepage_index.html 处呈现数字。

我找到了一个没有覆盖 get_context_data 的解决方案,但我解决了一半的问题。(我没有使用覆盖 get_context_data 方法)。 现在我可以在 homepage_index.html 中显示我想要的多个信息,如下所示:

from blog.models import Article
from polls.models import Question

from django.shortcuts import render

def homepage(request):
    list_of_title_article = Article.objects.all()
    list_of_title_question = Question.objects.all()

    return render(request, 'homepage_index.html',  {'latest_question_list': list_of_title_question, 'latest_article_list': list_of_title_article})

它看起来像我想要的那样工作,但我仍然没有我的对象的任何计数器方法。

现在我正在尝试使用 Question.objects.all().count() 并且我没有上下文可以传递到渲染器(请求,...)