2

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="#" class="list-group-item list-group-item-action bg-transparent border border-secondary">
                                                    <h class="mb-1">{{task.title}}</h>
                                                    <p class="mb-1">{{task.description}}</p>
                                                    <a class="mb-1">{{task.urgency}}</a>
                                                </a>
                                            {% endfor %}




                {% for post in object_list %}
                    <span>
                        <div class="card text-secondary-emphasis border border-secondary mb-4" style="background-color: #0d1117;">
                          <div class="card-header border border-secondary">{{post.type}}</div>
                          <div class="card-body">
                            <h5 class="card-title"style="color: #c0c0c0;">{{post.title}}</h5>
                            <p class="card-text">{{post.description}}</p>
                                <a href="{{post.link}}" class="btn btn-outline-primary">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 :)

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.