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

维杰

我正在开发一个 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"),
 ]

这是项目/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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我不能在两个渲染函数中传递一个函数?

为什么不能在一个函数中混合两个原语?

为什么两个if语句不能在一个函数中起作用?

为什么不能在一行中交换列表中的两个项目?

为什么tidyverse :: map不能在一个小标题中同时使用两个列表?

为什么不能在两个dom元素中追加同一个孩子?

为什么这两个路由器不能在同一个网络中工作?

为什么 SwiftUI 不能正确显示两个视图?

为什么我的if语句不能在两个嵌套执行中确定正确的输出?

为什么我的列表反向功能只能自己工作,而不能在另一个功能中工作?

为什么我的一个 JFrame 不能在外部 jar 文件上打开/显示?

为什么我不能两个接一个地等待?

为什么我不能将两个变量附加为一个元组

为什么我不能使用两个“if 语句”而不是一个 elif,如果两者中只有一个会执行?

为什么不能在一个事务中创建和删除表两次?

为什么不能在ruby中对一个实例两次使用相同的方法?

为什么在Java的一个文件中不能定义两个公共类?

当将应用程序部署为JAR时,为什么“ ..”(两个句点)不能在一个目录中工作?

为什么我不能按顺序在一个文件中初始化两个静态类变量,而不能初始化三个?

为什么我不能在 Chrome 中声明一个与函数体同名的 let 变量

为什么我不能在“ Prolog + C”中存储一个变量的地址?

为什么我不能在zsh中定义一个名为path的只读变量?

为什么我不能在Java中声明一个函数数组?

为什么我不能在另一个类中调用静态方法?

为什么我不能在工作目录中创建一个新文件?

为什么我不能在参数中做每一个?

为什么我不能在子字符串中捕获一个以上的数字?

为什么我不能在另一个文件中重载<<操作符?

为什么我的列表只删除了两个或多个相同值中的一个?