我正在django中做一個項目,在那里我根據(jù)評論中給出的評級創(chuàng)建了一個toplist。有沒有辦法獲得html中顯示的電影的評級?與 models.py 相比class Film(models.Model): title = models.CharField(max_length=100) title_short = models.CharField(max_length=17, default=None, null=True) plot = models.TextField() poster = models.ImageField(default="default.png", upload_to="posters") release_date = models.DateField(blank=True, null=True) date_posted = models.DateTimeField(default=timezone.now)class Review(models.Model): writer = models.ForeignKey(User, on_delete=models.CASCADE) reviewed_film = models.ForeignKey(Film, related_name='reviews', on_delete=models.CASCADE) title = models.CharField(max_length=100) content = models.TextField() rating = models.IntegerField( default=1, validators=[MinValueValidator(1), MaxValueValidator(5)] ) def __str__(self): # pragma: no cover return f"{self.reviewed_film.title} reviewed by {self.writer.username}"從排行榜.html{% extends "board/base.html" %}{% block content %}<h1>Toplist</h1> {% for film in films %} <div class="col s12 m7"> <h2 class="header"></h2> <div class="card horizontal"> <div class="card-image"> <img src="/media/{{film.poster}}"> </div> <div class="card-stacked"> <div class="card-content"> <h5>{{forloop.counter}}. {{film.title}}</h5> <p>{{}}/5.0</p> </div> <div class="card-action"> <a href="#">Read more</a> </div> </div> </div> </div> {% endfor %}{% endblock content %}
1 回答

浮云間
TA貢獻1829條經(jīng)驗 獲得超4個贊
您可以在查詢集中使用。例如:annotate
from django.db.models import Avg
queryset = Film.objects.annotate(total_rating=Avg('reviews_rating'))
,然后在您的 html 文件中
{% for film in films %}
<img src="/media/{{ film.poster }}">
{{ forloop.counter }}. {{ film.title }}
{{ film.total_rating }}/5.0
{% endfor %}
本文檔包含更多詳細(xì)信息。
添加回答
舉報
0/150
提交
取消