当我使用get方法时,我不存在匹配查询,但是应该有一个匹配查询

时间:2018-09-11 13:30:15

标签: python django

当我改用过滤器方法时,索引超出范围,我也不理解。我是否将整个视图,模型和网址设置错误?

在我的模型中。py

class Drama(models.Model):

class Season(models.Model):
drama = models.ForeignKey('Drama', on_delete=models.CASCADE)

class Content(models.Model):
season = models.ForeignKey('Season', on_delete=models.CASCADE)
now in my urls.py

如您所见,它们具有外键关系,我希望我的网址具有domain / kdrama / s1e1 所以我以以下方式设置了网址

path('<slug:slug1>/<slug:slug2>', views.contents, name='contents'),

我遇到的问题是在views.py中,它应该是用于检索数据

seasons method works fine
def seasons(request, slug):
    seasons = Season.objects.filter(drama__name=slug)
    return render(request, 'seasons.html', {
        'seasons':seasons,
        })
problem is this one, I want the contents to be filtered based on the seasons. So for instance, I have season1 under kdrama then the contents related to that filed to show up. 

我尝试过这种方式,但是我不确定为什么要获取匹配的查询不存在错误...。我像季节方法一样,根据季节名称过滤内容。这是数据库问题吗?还是我的逻辑非常错误?

def contents(request, slug1, slug2):
    contents = Content.objects.get(season__name=slug1)
    return render(request, 'contents.html',{
        'contents':contents,
        })

0 个答案:

没有答案