为什么我不能在一个模板中显示两个列表视图?

Why I can not display two listviews in one template?

我正在开发一个 Django 项目,管理员可以在该项目中将英雄横幅上传到网站,还可以在网站主页上上传通知。 所以我制作了列表视图来列出两个 HeroListView 和 NotificationListView。 但是第二个没有出现在页面中。 请告诉我如何解决这个问题。

这是 models.py 文件

from django.db import models
class Hero(models.Model):
   image = models.ImageField(upload_to="heros/")

class Notification(models.Model):
   notification = models.CharField(max_length=200)

这是 views.py 文件。

from django.views.generic import ListView
from .models import Hero, Notification

class HeroListView(ListView):
   model = Hero
   template_name = "home.html"
   context_object_name = "hero_list"

class NoticficationListView(ListView):
   model = Notification
   template_name = "home.html"
   context_object_name = "notification_list"

这是 app/urls.py 文件

from django.urls import path
from .views import HeroListView, NoticficationListView

urlpatterns = [
    path("", HeroListView.as_view(), name="home"),
    path("", NoticficationListView.as_view(), name="home"),
 ]

这是 project/urls.py 文件

from django.contrib import admin
from django.urls import path, include
from django.conf import settings 
from django.conf.urls.static import static

urlpatterns = [
   path('admin/', admin.site.urls),
   path("", include("heros.urls")),
   path("", include("products.urls")),
  ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

{% extends '_base.html' %}

{% block title %}Home{% endblock title %}

{% block content %}
{% load static %}
<div class="container mt-4">
    <div id="carouselExampleControls" class="carousel carousel-dark slide" data-bs-ride="carousel">
        <div class="carousel-inner">
          <div class="carousel-item active">
            <img src="{% static 'images/hero2.jpg' %}" class="d-block w-100" alt="..." style="max-height: 400px;">
          </div>
          {% for hero in hero_list %}
          <div class="carousel-item">
            <img src="{{ hero.image.url }}" class="d-block w-100" alt="..." style="max-height: 400px;">
          </div>
          {% endfor %}
        </div>
        <button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleControls" data-bs-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="visually-hidden">Previous</span>
          </button>
          <button class="carousel-control-next" type="button" data-bs-target="#carouselExampleControls" data-bs-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="visually-hidden">Next</span>
          </button>
      </div>
      <div class="div">
        {% for note in notification.list %}
        <p>{{ note.notification }}</p>
        {% endfor %}
      </div>
</div>
{% endblock content %}

请告诉我如何解决这个问题。

你可以这样做

class HeroListView(ListView):
    model = Hero
    template_name = "home.html"

    def get_context_data(self):
        context = super(HeroListView, self).get_context_data()
        context['hero_list'] = Hero.objects.all()
        context['notification_list'] = Notification.objects.all()
        
        return context

您不能同时访问两个或多个视图,因此在您的情况下,您可以删除 NoticficationListView 并仅使用 HeroListView 来呈现 hero_listnotification_list