Linked Questions

Popular Questions

How to insert a second model into my ListView

Asked by At

views.py

from django.shortcuts import render, get_object_or_404
    from library.models import Game
    from .models import Post
    from django.views.generic import (
        ListView
    )
    from django.template import context

    # Create your views here.
    def home(request):
        context = {
            'recent': Game.objects.all().order_by('post__date_posted')[:5],
            'posts': Post.objects.all()
        }
        return render(request, 'main/home.html', context)

    class TitlePostListView(ListView):
        model = Post
        template_name = 'main/title_posts.html'
        context_object_name = 'posts'


        def get_queryset(self):
            title = get_object_or_404(Game, title=self.kwargs.get('title'))
            return Post.objects.filter(game=title).order_by('-date_posted')[:5]

title_posts.html

{% extends "main/base.html" %}
{% load static %}
{% block styles %}
    <link rel="stylesheet" type="text/css" href="{% static 'main/title_posts.css' %}">
{% endblock styles %}

{% block content %}
        <style>
        body {
            background-image: url("{{ game.cover_display.url }}");
            background-repeat: no-repeat;
            background-size: 100% 1000px;
            background-color: #171717;
    }
    </style>
        <div class="container margin-top-300">
          <div class="row justify-content-center">
            <div class="col-3 text-center">
                <img src="{{ game.cover.url }}">
            </div>
            <div class="col">
              <p>{{ game.description| safe }}</p>
            </div>
        </div>
        <hr>

    {% for post in posts %}
        <div class="row">
          <div class="col-4 article-column-height text-center">
               <a href="#"><img class="article-image-height" src="{{ post.article_image.url }}"></a>
          </div>
          <div class="col-8 article-column-height">
              <h2><a class="article-title" href="#">{{ post.article_title }}</a></h2>
          </div>
        </div>
        <hr>
      </div>
    {% endfor %}
{% endblock content %}

models.py

class Post(models.Model):
    article_title = models.CharField(max_length=60, default="Article Title Place Holder")
    content = HTMLField(default="Article Content Pace Holder")
    date_posted = models.DateTimeField(default=timezone.now)
    game = models.ForeignKey('library.Game', on_delete=models.CASCADE)
    article_image = models.ImageField(default='/media/default.png')

class Game(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)
    cover = models.ImageField()
    cover_display = models.ImageField(default='default.png')
    developer = models.CharField(max_length=100)

Edit: I have a page that displays posts/articles for a certain video game. Currently my query returns the posts from the Post model for that video game. However, I am unsure of how to also display the video games description and images on that same page. The {{game.cover.url}}, ((game.description}}, and {{game.cover_display.url}} do not show up when I load the page because I am unsure how to also get that specific games objects from the Game model.

{% for post in posts %}
{% with post.game.set.all|first as game %}
    <style>
    body {
        background-image: url("{{ game.cover_display.url }}");
        background-repeat: no-repeat;
        background-size: 100% 1000px;
        background-color: #171717;
}
</style>
    <div class="container margin-top-300 black">
      <div class="row justify-content-center">
        <div class="col-3 text-center">
            <img src="{{ game.cover.url }}">
        </div>
        <div class="col">
          <p>{{ game.description| safe }}</p>
        </div>
    </div>
    {% endwith %}
    <hr>
    <div class="row">
      <div class="col-4 article-column-height text-center">
           <a href="#"><img class="article-image-height" src="{{ post.article_image.url }}"></a>
      </div>
      <div class="col-8 article-column-height">
          <h2><a class="article-title" href="#">{{ post.article_title }}</a></h2>
      </div>
    </div>
    <hr>
  </div>
{% endfor %}

Related Questions