Home > other >  How to add multiple models to a single listview?
How to add multiple models to a single listview?

Time:12-31

i need to be able to display information from multiple models in a single listview but haven't a clue how to go about doing this any help would be very helpful...

here is my code for me views.py:


from django.shortcuts import render, redirect
from django.views.generic import ListView, DetailView
from . models import Post, Task
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator



@method_decorator(login_required, name='dispatch')
class HomeView(ListView):
    template_name = 'home.html'
    model = Post



@method_decorator(login_required, name='dispatch')
class Task(ListView):
    model = Task
    template_name = 'tasks.html'

here is my code for me models.py:

from django.db import models
from django.contrib.auth.models import User

class Post(models.Model):
    type = models.CharField(max_length=255)
    title = models.CharField(max_length=255)
    link = models.CharField(max_length=255)
    auther = models.ForeignKey(User, on_delete=models.CASCADE)
    description = models.TextField()


    def __str__(self):
        return self.title   ' | '   str(self.auther)


class Task(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    urgency = models.CharField(max_length=255)
    title = models.CharField(max_length=255)
    description = models.TextField()

    def __str__(self,):
        return str(self.user.username)   ' | '   'Tasks'

here is my html:

        {% for task in object_list %}
                                                <a href="#" >
                                                    <h >{{task.title}}</h>
                                                    <p >{{task.description}}</p>
                                                    <a >{{task.urgency}}</a>
                                                </a>
                                            {% endfor %}




                {% for post in object_list %}
                    <span>
                        <div  style="background-color: #0d1117;">
                          <div >{{post.type}}</div>
                          <div >
                            <h5 style="color: #c0c0c0;">{{post.title}}</h5>
                            <p >{{post.description}}</p>
                                <a href="{{post.link}}" >Read More</a>
                            </div>
                        </div>
                </span>
            {% endfor %}

if you need any more info on what im trying to achieve or need to see any other code snippets just let me know :)

CodePudding user response:

I guess, TemplateView could be more suitable for you. If you aren`t going to use pagination, ListView is not really what you need. Here is how your code could look:

@method_decorator(login_required, name='dispatch')
class HomeView(TemplateView):
    template_name = 'home.html'
    
    def get(self, request, *args, **kwargs):
        self.extra_context = {
            "posts": Post.objects.all(),
            "tasks": Task.objects.all(),
        }
        return self.render_to_response(self.extra_context)

And, therefore, you will have to change tags in your templates and replace object_list to posts and tasks respectfully. It should look like this:

{% for post in posts %}
{% for task in tasks %}

Hope it will help you!

  • Related