本文介绍了在网址中组合多个子词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用两个子段来生成某种类型的帖子的网址.我的网站按类别划分,每个人都有一个或多个帖子.

I'm trying to use two slug for generate the urls of a type of post. My site is divided in category and everyone of these have one or more 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'),

当我使用第二条路径时,向我显示以下内容:

When I use the second couple of path it's shown to me this:

带有关键字参数'{'slug_post'的'single_blog_post'的反向: 找不到'rete-dei-sottoservizi-quadro-normativo'}. 1个模式 尝试过: ['blog \/(?P [-a-zA-Z0-9 _] +)\/(?P [-a-zA-Z0-9 _] +)\/$']

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})

此答案中

In this answer is explained how is possible to do the same thing that I try to do, but for my case don't work fine and I don't understand why.

推荐答案

但是正如您所说,URL包含两个子段,因此您需要在反向调用中将它们都传递.并且您需要使用与URL模式本身相同的名称.

But just like you said, the URL contains two slugs, so you need to pass them both in the reverse call. And you need to use the same names you have used in the URL pattern itself.

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

这篇关于在网址中组合多个子词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!