问题描述
我正在尝试创建一个卡通流媒体网站,当用户点击他们选择的卡通(例如:口袋妖怪)时,他们可以看到季节列表以及卡通的详细信息。
from django.db import models
class Cartoon(models.Model):
name = models.CharField(max_length=200)
cover = models.ImageField()
description = models.TextField()
start_date = models.CharField(max_length=50)
end_date = models.CharField(max_length=50)
def __str__(self):
return self.name
class CartoonSeason(models.Model):
cartoon = models.ForeignKey(Cartoon,null=True,on_delete=models.SET_NULL)
number = models.IntegerField()
season_cover = models.ImageField(blank=True,null=False)
season_name = models.CharField(max_length=200,blank=True,null=True)
season_description = models.TextField(blank=True,null=False)
这里我使用外键将 Cartoon 模型与 CartoonSeason 模型关联起来,因此当添加新季节时,它会自动与相应的 Cartoon 关联
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import ListView,DetailView
from .models import CartoonSeason,Cartoon
class CartoonListView(ListView):
model = Cartoon
template_name = "index.html"
class CartoonSeasonView(DetailView):
queryset = CartoonSeason.objects.filter()
model = CartoonSeason
template_name = "season.html"
我使用详细视图来显示 CartoonSeason
模型,以便它也显示卡通详细信息,但是当我尝试加载季节时,它只显示主键为 1 的季节。我希望它显示添加到卡通中的所有季节。
这是我的seasons.html
{% extends 'base.html' %}
{% block title %}
test
{% endblock %}
{% block content %}
<main>
<section class="cartoon-description">
<div class="season_head">
<img src="{{object.cartoon.cover.url}}" width="260px" alt="">
<div class="cartoon-name">
<h1>{{object.cartoon.name}}</h1>
<small >{{object.cartoon.start_date}} - {{object.cartoon.end_date}}</small>
<br>
<div class="description">
<strong>Description:</strong>
<br>
<p>{{object.cartoon.description}}</p>
</div>
</div>
</div>
</section>
<hr>
</main>
{% endblock %}
这是我的 urls.py
from .views import CartoonListView,CartoonSeasonView
urlpatterns = [
path('',CartoonListView.as_view(),name="home"),path('cartoon/<int:pk>',CartoonSeasonView.as_view(),name="cartoon"),]
这是我的主要模板
{% extends 'base.html'%}
{% block title %}
Home - CartoonsPalace
{% endblock %}
{% block content %}
<main>
<section class="cartoon-list">
<div class="select-text">
<h1>Pick Your Cartoon Series of Choice:-</h1>
</div>
{% for cartoon in object_list %}
<div class="list">
<a href="{% url 'cartoon' cartoon.pk %}"><div class="list-object">
<img src="{{cartoon.cover.url}}" alt="" width="184px">
<h3>{{cartoon.name}}</h3>
<span>{{cartoon.start_date}} - {{cartoon.end_date}}</span>
</div>
</a>
</div>
{% endfor %}
</section>
</main>
{% endblock %}
任何帮助将不胜感激。
解决方法
好的,当您想通过外键访问与另一个对象相关的所有对象时,您有两个简单的选择: 1.使用卡通设置查询(逆向关系):
cartoon = Cartoon.objects.get(id = 'pk') # get the pk by passing in url
cartoon_season = cartoon.cartoonseason_set.all()
# object.(name of model CartoonSeason must be in lowercase )._set.all()
或使用 CartoonSeason 设置查询(推荐):
#before this you must have the id of the cartoon :
cartoon = Cartoon.objects.get(id = pk)
seasons = CartoonSeason.objects.filter(cartoon = cartoon)
我不知道你的卡通模板或网址,但我假设在你展示卡通细节的卡通模板中,你有一个指向卡通季节的链接,所以你应该在那里传递卡通的id:{{ 1}}
并且在您的网址中,您可以在季节详细信息中使用此 ID:
但请注意,当您使用 DetailView 并希望使用通过 url 传递的 id 时,您应该像这样定义 {{cartoon.id}}
:
get_queryset
并记住用户不再需要 class CartoonSeasonView(DetailView):
model = CartoonSeason
template_name = "season.html"
def get_queryset(self,*args,**kwargs):
#before this you must have the id of the cartoon :
cartoon = Cartoon.objects.get(id = self.kwargs['pk'])
seasons = CartoonSeason.objects.filter(cartoon = cartoon)
。
通过这个你可以在卡通季页面中显示卡通或海报的名称。
请记住,我们使用详细视图是为了仅显示一个对象的详细信息,而不是 7 个对象(例如卡通季)。您可以使用 ListView,或者如果您想在一个模板中显示不同的数据,请使用 TemplateView。