Why I can not display two listviews in one template?


I am working on a Django project where admin can upload a hero banner to the site and can also upload a notification on the home page of the site.
So I made listviews for listing the two HeroListView and NotificationListView for that.
But the second one is not appearing in the page.
Please tell me how to solve this issue.

This is the models.py file

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)

This is the views.py file.

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"

this is the app/urls.py file

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

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

this is the project/urls.py file

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;">
          {% for hero in hero_list %}
          <div class="carousel-item">
            <img src="{{ hero.image.url }}" class="d-block w-100" alt="..." style="max-height: 400px;">
          {% endfor %}
        <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 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>
      <div class="div">
        {% for note in notification.list %}
        <p>{{ note.notification }}</p>
        {% endfor %}
{% endblock content %}

please tell me how to solve this issue.


You can do it this way

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

You can’t access two or multiple views at the same time, so in your case, you can remove NoticficationListView and use only HeroListView to render both hero_list and notification_list

Answered By – Adil Mohak

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published