使用基于 Class 视图的带有类别 slug 和 Post slug 的网址
Urls with Categories slug and Post slug using Class Based View
我正在开发一个简单的博客来学习 Django。我希望每个帖子都有这样的路径:
- /category-1/title-post
- /category-2/title-post
- etc..
低于urls.py
from django.urls import include, path
from .views import CategoryList, PostList, SingleCategory, SinglePost, SingleTag, TagList
urlpatterns = [
path("", PostList.as_view(), name="list_post"),
path("<slug:slug>", SinglePost.as_view(), name="single_post"),
path("tags/", TagList.as_view(), name="list_tag"),
path("tags/<slug:slug>", SingleTag.as_view(), name="single_tag"),
path("categories/", CategoryList.as_view(), name="list_category"),
path("categories/<slug:slug>", SingleCategory.as_view(), name="single_category"),
]
和views.py
from django.shortcuts import render
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import Category, Post, Tag
# Create your views here.
class CategoryList(ListView):
model = Category
context_object_name = 'category_list'
template_name = "list_category.html"
class SingleCategory(DetailView):
model = Category
template_name = "single_category.html"
class PostList(ListView):
model = Post
queryset = Post.objects.order_by('-id')
context_object_name = 'post_list'
template_name = "list_post.html"
paginate_by = 4
class SinglePost(DetailView):
model = Post
template_name = "single_post.html"
class TagList(ListView):
model = Tag
context_object_name = 'tag_list'
template_name = "list_tag.html"
class SingleTag(DetailView):
model = Tag
template_name = "single_tag.html"
这里models.py
class Category(models.Model):
category_name = models.CharField(
max_length=50,
)
slug = models.SlugField(
unique=True,
)
def __str__(self):
return self.category_name
def get_absolute_url(self):
return reverse("single_category", kwargs={"slug": self.slug})
class Post(models.Model):
title = models.CharField(
max_length=50,
)
slug = models.SlugField(
unique=True,
)
content = models.TextField()
tag = models.ManyToManyField(
Tag,
related_name="tag_set",
)
category = models.ForeignKey(
Category,
on_delete=models.CASCADE,
related_name="category_set",
)
highlighted = models.BooleanField(
default=False,
)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("single_post", kwargs={"slug": self.slug})
class Meta:
ordering = ['-id']
我不明白如何更改“category-slug/” 中的路径 "categories/"。我想为 类别/ 做同样的事情,必须在 category-slug/post-slug.
中更改
如何使用基于 Class 的视图执行此操作?
您可以在 URL 中定义任意数量的参数。然后你需要覆盖 get_object
以通过 slug 和类别获得相关的 post。
path('<slug:category_slug>/<slug:post_slug>', SinglePostByCategory.as_view(), 'single_post_by_category')
...
class SinglePostByCategory(DetailView):
def get_queryset(self):
return get_object_or_404(Post,
category__slug=self.kwargs['category_slug'],
slug=self.kwargs['post_slug']
)
我正在开发一个简单的博客来学习 Django。我希望每个帖子都有这样的路径:
- /category-1/title-post
- /category-2/title-post
- etc..
低于urls.py
from django.urls import include, path
from .views import CategoryList, PostList, SingleCategory, SinglePost, SingleTag, TagList
urlpatterns = [
path("", PostList.as_view(), name="list_post"),
path("<slug:slug>", SinglePost.as_view(), name="single_post"),
path("tags/", TagList.as_view(), name="list_tag"),
path("tags/<slug:slug>", SingleTag.as_view(), name="single_tag"),
path("categories/", CategoryList.as_view(), name="list_category"),
path("categories/<slug:slug>", SingleCategory.as_view(), name="single_category"),
]
和views.py
from django.shortcuts import render
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import Category, Post, Tag
# Create your views here.
class CategoryList(ListView):
model = Category
context_object_name = 'category_list'
template_name = "list_category.html"
class SingleCategory(DetailView):
model = Category
template_name = "single_category.html"
class PostList(ListView):
model = Post
queryset = Post.objects.order_by('-id')
context_object_name = 'post_list'
template_name = "list_post.html"
paginate_by = 4
class SinglePost(DetailView):
model = Post
template_name = "single_post.html"
class TagList(ListView):
model = Tag
context_object_name = 'tag_list'
template_name = "list_tag.html"
class SingleTag(DetailView):
model = Tag
template_name = "single_tag.html"
这里models.py
class Category(models.Model):
category_name = models.CharField(
max_length=50,
)
slug = models.SlugField(
unique=True,
)
def __str__(self):
return self.category_name
def get_absolute_url(self):
return reverse("single_category", kwargs={"slug": self.slug})
class Post(models.Model):
title = models.CharField(
max_length=50,
)
slug = models.SlugField(
unique=True,
)
content = models.TextField()
tag = models.ManyToManyField(
Tag,
related_name="tag_set",
)
category = models.ForeignKey(
Category,
on_delete=models.CASCADE,
related_name="category_set",
)
highlighted = models.BooleanField(
default=False,
)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("single_post", kwargs={"slug": self.slug})
class Meta:
ordering = ['-id']
我不明白如何更改“category-slug/” 中的路径 "categories/"。我想为 类别/ 做同样的事情,必须在 category-slug/post-slug.
中更改如何使用基于 Class 的视图执行此操作?
您可以在 URL 中定义任意数量的参数。然后你需要覆盖 get_object
以通过 slug 和类别获得相关的 post。
path('<slug:category_slug>/<slug:post_slug>', SinglePostByCategory.as_view(), 'single_post_by_category')
...
class SinglePostByCategory(DetailView):
def get_queryset(self):
return get_object_or_404(Post,
category__slug=self.kwargs['category_slug'],
slug=self.kwargs['post_slug']
)