一个网址中有两个子弹,kwargs和args

时间:2019-05-20 16:31:49

标签: python django

我希望自己的网址像“ slug / slug /” 如果没有将它们组合在一起,则每个子弹都可以正常工作,但是当我尝试将两者结合在一起时,就会遇到问题。我想我可以为此使用args和kwargs,但是我不知道如何应用它。

path('series/<slug>/', views.season_pg, name='season_detail'),

def season_pg(request, slug, slug):
    series = Series.objects.get(slug=slug)

    season_nr = Season.objects.get(slug=slug)
    content_dict = {
        'season_nr':season_nr
    }
    return render(request, 'series.html', content_dict)

Models.py

class Series(models.Model):
    name = models.CharField(max_length=128, unique=True)
    genre = models.CharField(max_length=128, default=1)
    tv_or_movie = models.CharField(max_length=128, default=1)
    period = models.CharField(max_length=128, default=1)
    descritpion = models.TextField()
    slug = models.SlugField(unique=True)

    #img
    #video

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return "/%s/" %self.slug




class Season(models.Model):
    series = models.ForeignKey(Series, on_delete=models.CASCADE, blank=True, null=True)
    season_nr = models.CharField(max_length=128, default=1)
    date = models.DateTimeField(auto_now_add = True)
    slug = models.SlugField(unique=True)

    def __str__(self):
        return str(self.season_nr)

1 个答案:

答案 0 :(得分:0)

path('series/<slug:serie>/<slug:season>/', views.season_pg, name='season_detail'),

def season_pg(request, serie, season):
    try:
        series = Series.objects.get(slug=serie)
    except Series.DoesNotExist:
        # if no rows found
        series = ''
    try:
        season_nr = Season.objects.get(slug=season)
    except Season.DoesNotExist:
        # if no rows found
        season_nr = ''

    context = {
        'series': series,
        'season_nr':season_nr
    }
    return render(request, 'series.html', context)