在 url 中组合多个 slug

Combining more than one slug in the url

我正在尝试使用两个 slug 来生成 post 类型的 url。我的网站按类别划分,每个人都有一个或多个 post。

views.py

def singlePost(request, slug_post):
    blogpost = get_object_or_404(BlogPost, slug_post=slug_post)
    context = {"blogpost": blogpost}
    return render(request, "blog/single_post.html", context)


def singleCategory_postList(request, slug_category):
    category = get_object_or_404(Category, slug_category=slug_category)
    blogpost = BlogPost.objects.filter(category=category)
    context = {
            "category": category,
            "blogpost": blogpost,
            }
    return render(request, "blog/single_category.html", context)
我用的

urls.py

path("category/<slug:slug_category>/", views.singleCategory_postList, name="single_category"),
path("<slug:slug_post>/", views.singlePost, name='single_blog_post'),
我想使用

urls.py

path("<slug:slug_category>/", views.singleCategory_postList, name="single_category"),
path("<slug:slug_category>/<slug:slug_post>/", views.singlePost, name='single_blog_post'),

当我使用第二对路径时,它向我显示了这个:

NoReverseMatch at /blog/gis/

Reverse for 'single_blog_post' with keyword arguments '{'slug_post': 'rete-dei-sottoservizi-quadro-normativo'}' not found. 1 pattern(s) tried: ['blog\/(?P[-a-zA-Z0-9_]+)\/(?P[-a-zA-Z0-9_]+)\/$']

models.py

class Category(models.Model):
    category_name = models.CharField(
                        max_length=50,
                        verbose_name="Categorie",
                        help_text="Every category must be not longer then 50 characters",
                        unique=True,
                        )
    slug_category = models.SlugField(
                verbose_name="Slug",
                unique=True,
                help_text="Slug is a field in autocomplete mode, but if you want you can modify its contents",
                )
....

    def __str__(self):
        return self.category_name

    def get_absolute_url(self): 
        return reverse("single_category", kwargs={"slug_category": self.slug_category})


class BlogPost(ModelPost, TimeManager):
    category = models.ForeignKey(
                    Category,
                    on_delete=models.CASCADE,
                    related_name="category_set",
                    verbose_name="Categorie",
                    help_text="Select a category for this article.You can select only one category.",
                    )
    keyconcepts = models.ManyToManyField(
                    KeyConcept,
                    related_name="keyconcept_blog_set",
                    help_text="Select a key concept for this article. You can select one or more key concepts.",
                    verbose_name="Concetti chiave",
                    )
.....

    def get_absolute_url(self): 
        return reverse("single_blog_post", kwargs={"slug_post": self.slug_post})

this answer 中解释了如何做与我尝试做的相同的事情,但对于我的情况来说效果不佳,我不明白为什么。

但是就像你说的,URL 包含两个 slug,所以你需要在反向调用中传递它们。并且您需要使用与 URL 模式本身相同的名称。

return reverse("single_blog_post", kwargs={"slug": self.slug_post, "slug_category": self.category.slug_category})

多亏了 Daniel Roseman 的指示,我通过稍作改动解决了我的问题。

这是新的 singlePost 函数:

def singlePost(request, slug_post, slug_category):
    category = get_object_or_404(Category, slug_category=slug_category)
    blogpost = get_object_or_404(BlogPost, slug_post=slug_post)
    category_blogpost = BlogPost.objects.filter(category=category)
    context = {"category": category, "blogpost": blogpost, "category_blogpost": category_blogpost}
    return render(request, "blog/single_post.html", context)

下面是 Daniel Roseman 的代码:

return reverse("single_blog_post", kwargs={"slug_post": self.slug_post, "slug_category": self.category.slug_category})

现在我可以在我的网站上使用两个 slug