假设当前日期是 Monday ,并且上面的 timeslots 不是或 这是我的 models.py : 从django.db导入模型从django.contrib.auth.models导入AbstractUser从PIL导入图像类TimeSlot(models.Model):moviename = models.CharField(max_length = 100)日= 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.day}'的f'{self.moviename}时隙Movie(models.Model)类:标题= models.CharField(max_length = 100)描述= models.TextField()release_date = models.CharField(max_length = 20)类型= models.CharField(max_length = 100)time_slot = models.ManyToManyField(TimeSlot,related_name = None)available_seats = models.PositiveSmallIntegerField(默认= 300)ticket_price = models.DecimalField(max_digits = 6,decimal_places = 2)图片= models.ImageField(默认='default.jpg',upload_to ='媒体/电影_海报')def __str __():返回f'{self.title}信息'def save(self,* args,** kwargs):超级(电影,自我).save(* args,** kwargs)图片= Image.open(self.image.path)如果image.height>200或image.width>200:output_size =(200,200)image.thumbnail(output_size)image.save(self.image.path) 这是我的 views.py :来自django.shortcuts的 导入渲染从.models导入Movie,TimeSlotpagetitle ='在线订购车票'def home(要求):上下文= {电影列表":Movie.objects.all(),'timeslot':TimeSlot.objects.all(),标题":pagetitle}返回render(请求,'movies/home.html',上下文)def about(要求):返回render(request,'movies/about.html') 这是我的home.html模板: {%封锁内容%}{%代表电影列表中的电影%%}< 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">显示于:{{movie.release_date}}</small></div>< img src ="{{movie.image.url}}""alt =">< p class ="article-content">< strong>电影说明:</strong> {{movie.description}}< br>< strong>类型:</strong> {{movie.genre}}< br>< strong>时隙:</strong>< br>< strong>可用座位:</strong> {{movie.available_seats}}< br>< strong>机票价格:</strong> {{movie.ticket_price}}< br></p></div></article>{%endfor%}{%endblock%} 在这件事奏效之前,谁能指导我?python新手,谢谢!这是我的 django管理员内部和 Movie 应用程序下的外观: 时隙表中的数据如下: 当我单击标题时: 电影"表中的内容如下: 解决方案如果您查看文档中的多对多示例,您可以使用 movie_instance.time_slot.all()访问时隙.在您看来,您可以省略 TimeSlot 查询集: def home(request):上下文= {电影列表":Movie.objects.all(),标题":pagetitle}对于上下文中的m ['movielist']:#用于调试,请稍后删除print(m.time_slot.all())#用于调试,稍后删除返回render(请求,'movies/home.html',上下文) 在您的模板中,您可以尝试 < strong>时间段:</strong>{%for movie.time_slot.all%中的t}{{t.timeslot1}}{{t.timeslot2}}...{%endfor%} 或者您可以在模型中添加一个方法来过滤和准备所需的字符串,然后在模板中为每个电影实例调用该方法. class Movie(models.Model):def get_monday_time_slots(self):返回self.time_slot.filter(day ='monday') 有帮助吗?I am trying to make a ticketing site for a Cinema which my project in school, and I made two models, 1 is Movie and 2 TimeSlot. Movie.time_slot has a manytomanyrelation to TimeSlot table. What I want to happen is, display the timeslot for the Movie depending on the Movie.title and on what day it is. For example, i want the Time Slots: row below to display:Monday: timeslot1.value, timeslot2.value, timeslot3.value, timeslot4.valuegiven that the current day is Monday and timeslots above were not empty or nullHere is my models.py:from django.db import modelsfrom django.contrib.auth.models import AbstractUserfrom PIL import Imageclass 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)Here is my views.py:from django.shortcuts import renderfrom .models import Movie,TimeSlotpagetitle = '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')And here is my home.html template:{% 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 %}Anyone who can guide me until this thing works? Newbie in python here, thanks!Here is what it looks like inside my django administrator and under Movie app:And inside the Time slots table here are the data:when i click the title:Here is what it looks like inside the Movie table: 解决方案 If you look at the many-to-many example in the docs, you could access to time slots using movie_instance.time_slot.all().In your view you could omit the TimeSlot queryset: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)And in your template you can try<strong>Time Slots:</strong>{% for t in movie.time_slot.all %} {{ t.timeslot1 }} {{ t.timeslot2 }} ...{% endfor %}Or you could add a method to your model to filter and prepare a string with what you need and then just call that method in your template for each movie instance.class Movie(models.Model): def get_monday_time_slots(self): return self.time_slot.filter(day='monday')Does that help? 这篇关于如何获取表中的整行并将其显示在模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-25 09:38