如何将第二个模型插入我的ListView

时间:2019-02-04 03:10:52

标签: django django-models

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)

编辑:我有一个页面,显示某个视频游戏的帖子/文章。目前,我的查询返回的是来自该视频游戏的Post模型的帖子。但是,我不确定如何在同一页面上显示视频游戏说明和图像。加载页面时,{{game.cover.url}},((game.description}}和{{game.cover_display.url}}不会显示,因为我不确定如何也可以获取特定的游戏对象来自游戏模型。

{% 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 %}

1 个答案:

答案 0 :(得分:0)

您正在使用ForeignKey。所以在循环中使用这样的

event

由于您已经按游戏进行过滤,因此只能将游戏添加到get_context_data()方法中

def create
  event = Event.new(event_params)
  location = event.location.build() #or event.location.build(location_parms)
  location.save #will create record in events as well as location associated with the event
end

private
  def event_params
     #Whitelist only events attributes
     params.require(:event).permit(:name, :time, :organizer_id, attachments: [])
  end