如何获取表中的整行并将其显示在模板中

时间:2019-04-04 18:30:20

标签: python django

我正在尝试为一个电影院制作票务站点,这是我在学校的项目,我制作了两个模型,一个是Movie,两个是TimeSlotMovie.time_slotmanytomany表具有TimeSlot关系。

我想发生的是,根据Movie.titleday来显示电影的时隙。

例如,我希望下面的Time Slots:行显示:

星期一:timeslot1.valuetimeslot2.valuetimeslot3.valuetimeslot4.value

假设当前日期不在Mondaytimeslots以上,empty

enter image description here

这是我的null

models.py

这是我的 from django.db import models from django.contrib.auth.models import AbstractUser from PIL import Image class TimeSlot(models.Model): moviename = models.CharField(max_length=100) day = models.CharField(max_length = 9) timeslot1 = models.CharField(max_length = 20,blank = True, null = True) timeslot2 = models.CharField(max_length = 20,blank = True, null = True) timeslot3 = models.CharField(max_length = 20,blank = True, null = True) timeslot4 = models.CharField(max_length = 20,blank = True, null = True) timeslot5 = models.CharField(max_length = 20,blank = True, null = True) timeslot6 = models.CharField(max_length = 20,blank = True, null = True) def __str__(self): return f'{self.moviename} timeslot for {self.day}' class Movie(models.Model): title = models.CharField(max_length= 100) description = models.TextField() release_date = models.CharField(max_length=20) genre = models.CharField(max_length=100) time_slot = models.ManyToManyField(TimeSlot, related_name=None) available_seats = models.PositiveSmallIntegerField(default= 300) ticket_price = models.DecimalField(max_digits=6,decimal_places=2) image = models.ImageField(default = 'default.jpg', upload_to = 'media/movie_posters') def __str__(self): return f'{self.title} Info' def save(self, *args, **kwargs): super(Movie, self).save(*args, **kwargs) image = Image.open(self.image.path) if image.height > 200 or image.width > 200: output_size = (200, 200) image.thumbnail(output_size) image.save(self.image.path)

views.py

这是我的home.html模板:

from django.shortcuts import render
from .models import Movie,TimeSlot


pagetitle = 'Order Tickets Online'

def home(request):
    context = {
        'movielist': Movie.objects.all(),
        'timeslot': TimeSlot.objects.all(),
        'title': pagetitle
    }
    return render(request, 'movies/home.html', context)

def about(request):
    return render(request, 'movies/about.html')

谁能指导我直到这件事起作用? python新手,谢谢!

这是我的 {% block content %} {% for movie in movielist %} <article class="media content-section"> <div class="media-body"> <div class="article-metadata"> <a class="mr-2" href="#" style="font-size: 30px"> {{ movie.title}}</a> <small class="text-muted" >Showing on: {{ movie.release_date }}</small> </div> <img src="{{movie.image.url}}" alt=""> <p class="article-content"> <strong>Movie Description: </strong>{{ movie.description }} <br> <strong>Genre: </strong>{{ movie.genre }} <br> <strong>Time Slots: </strong><br> <strong>Available Seats: </strong>{{ movie.available_seats }} <br> <strong>ticket_price: </strong>{{ movie.ticket_price }} <br> </p> </div> </article> {% endfor %} {% endblock %} 内部和django administrator应用程序下的样子: enter image description here

Movie表中是以下数据: enter image description here

当我单击标题时: enter image description here

这是Movie表中的外观:

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您查看many-to-many example in the docs,则可以使用movie_instance.time_slot.all()访问时隙。

您认为您可以忽略TimeSlot查询集:

def home(request):
    context = {
        'movielist': Movie.objects.all(),
        'title': pagetitle
    }
    for m in context['movielist']:    # for debug, remove later
        print(m.time_slot.all())      # for debug, remove later
    return render(request, 'movies/home.html', context)

在您的模板中,您可以尝试

<strong>Time Slots:</strong>
{% for t in movie.time_slot.all %}
    {{ t.timeslot1 }}
    {{ t.timeslot2 }}
    ...
{% endfor %}

或者您可以在模型中添加一个方法来过滤和准备所需的字符串,然后在模板中为每个电影实例调用该方法。

class Movie(models.Model):
    def get_monday_time_slots(self):
        return self.time_slot.filter(day='monday')

有帮助吗?