@Rado: W pliku views.html
staram się pobrać wszystkie returny z moich obiektów. Mam ich trzy a może będzie cztery.
def home(request):
context = {
'highlights0':
{Movie.objects.all()[0]: Movie.objects.all()[0].cinemas_that_play_it()},
'highlights1':
{Movie.objects.all()[1]: Movie.objects.all()[1].cinemas_that_play_it()},
'highlights2':
{Movie.objects.all()[2]: Movie.objects.all()[2].cinemas_that_play_it()},
}
I wyświetlić w HTML
{% extends "cinemas/base.html" %}
{% block title %}Sieć kin Cinemas{% endblock %}
{% block content %}
<h1>Sieć kin Cinemas:</h1>
{% for x in highlights0.items %}
{% for name, details in highlights0.items %}
{%for detail in details%}{{detail}} {%endfor%}
{% endfor %}
</p>
{% for name, details in highlights1.items %}
{%for detail in details%}{{detail}} {%endfor%}
{% endfor %}
</p>
{% for name, details in highlights2.items %}
{%for detail in details%}{{detail}} {%endfor%}
{% endfor %}
{% endblock %}
Z tym, że tutaj na sztywno zakładam, że będę trzy rzeczy do wyświetlenia a myślę o pętli, która sama to wykryje i zrobić output odpowieniej ilości elementów.
Tak wygląda models.py
from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator
class Cinema(models.Model):
name = models.CharField(max_length=100)
seats = models.PositiveSmallIntegerField(validators=[MinValueValidator(10)])
def __str__(self):
return self.name
class Movie(models.Model):
title = models.CharField(max_length=100)
minimum_age = models.PositiveSmallIntegerField(validators=[MaxValueValidator(21)])
def __str__(self):
return self.title
def cinemas_that_play_it(self):
return self.projection_set.all()
class Projection(models.Model):
cinema = models.ForeignKey(Cinema, on_delete=models.CASCADE)
movie = models.ForeignKey(Movie, on_delete=models.CASCADE)
date = models.DateTimeField()
published = models.BooleanField()
tickets_available = models.PositiveSmallIntegerField()
def __str__(self):
return f"{self.movie.title} at {self.cinema.name} on {self.date.strftime('%d.%m.%Y %H:%M:%S')}"