在网址中组合多个子词

时间:2019-03-17 11:05:11

标签: python django django-urls slug django-2.1

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

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'),

当我使用第二条路径时,显示如下:

  

/ blog / gis /

中的NoReverseMatch      

带有关键字参数'{'slug_post'的'single_blog_post'的反向:   找不到'rete-dei-sottoservizi-quadro-normativo'}。 1个图案   尝试过:   ['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中说明了如何做与我尝试做的事情相同的事情,但是对我而言,它不能很好地工作,我也不明白为什么。

2 个答案:

答案 0 :(得分:2)

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

$ npm run build 
$ sudo vcg

答案 1 :(得分:0)

由于丹尼尔·罗斯曼(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})

现在我可以在我的网站中使用两个子弹