如何获得从网址即路径不同的模型蛞蝓( / /)?

问题描述 投票:0回答:1

我试着去得到看起来像这样WWW.domain.com/slug1/slug2,其中slug1是一决高下的外键一个UR模式。认为它像library.com//。作者和书籍是两种不同的模式,都与自己的蛞蝓。有没有一种方法,我可以从笔者导入塞书的的DetailView,然后用它的网址为这本书的DetailView?

这是我想象的样子路径:

    path('brands/<slug:brand_slug>/<slug:model_slug>', views.Brand_ModelsDetailView.as_view(), name='model-detail'),

这是我的模型:

class Brand(models.Model):
    brand_name = models.CharField(
        max_length=50, help_text='Enter the brand name',)  
    slug = AutoSlugField(populate_from='brand_name', default = "slug_error", unique = True, always_update = True,)


    def get_absolute_url(self):
        """Returns the url to access a particular brand instance."""
        return reverse('brand-detail', kwargs={'slug':self.slug})

    def __str__(self):
        return self.brand_name

class Brand_Models(models.Model):
    name = models.CharField(max_length=100)
    brand = models.ForeignKey('Brand', on_delete=models.SET_NULL, null=True)
    slug = AutoSlugField(populate_from='name', default = "slug_error_model", unique = True, always_update = True,)


    def get_absolute_url(self):
        """Returns the url to access a particular founder instance."""
        return reverse('model-detail', kwargs={'slug':self.slug})

    def __str__(self):
        return self.name

我当时的看法当前的尝试:

class Brand_ModelsDetailView(generic.DetailView):
    model = Brand_Models


    def get_queryset(self):
        qs = super(Brand_ModelsDetailView, self).get_queryset()
        return qs.filter(
            brand__slug=self.kwargs['brand_slug'],
            slug=self.kwargs['model_slug']
        )

编辑:

class RefrenceDetailView(generic.DetailView):
   model = Refrence

   def get_queryset(self):
       qs = super(RefrenceDetailView, self).get_queryset()
       return qs.filter(
            brand__slug=self.kwargs['brand_slug'],
            model__slug=self.kwargs['model_slug'],
        slug = self.kwargs['ref_slug']
    )
django url slug
1个回答
0
投票

你的错误就是用Brand_Models的get_absolute_url方法;因为详细的网址有两个蛞蝓,你需要通过他们既reverse那里。

return reverse('model-detail', kwargs={'brand_slug': self.brand.slug, 'model_slug':self.slug})
© www.soinside.com 2019 - 2024. All rights reserved.