单个 post 类别的重定向不正确
Incorrect redirection for a single post category
我正在开发我的个人博客;它只有两个类别,我想为这两个类别提供一个特定的 post 列表。
出于这个原因,我展开了 get_absolute_url,如下所示:
from django.db import models
from django.urls import reverse
CATEGORY_CHOICES = (
('G.I.S.', 'G.I.S.'),
('Sustainable Mobility', 'Sustainable Mobility'),
)
class Blog(models.Model):
"""
Blog's post definition
"""
title = models.CharField(
max_length=70,
unique=True,
)
slug = models.SlugField(
unique=True,
)
contents = models.TextField()
publishing_date = models.DateTimeField()
category = models.CharField(
max_length=50,
choices=CATEGORY_CHOICES,
)
def __str__(self):
return self.title
def get_absolute_url(self):
if Blog.objects.filter(category="G.I.S."):
return reverse("gis_single_post", kwargs={"slug": self.slug})
if Blog.objects.filter(category="Sustainable Mobility"):
return reverse("sumo_single_post", kwargs={"slug": self.slug})
下面可以看到views.py;它具有基于类别的不同模型:
from django.shortcuts import render
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import Blog
class GisSinglePost(DetailView):
model = Blog
queryset = Blog.objects.filter(category="G.I.S.")
context_object_name = 'post'
template_name = "gis_single_post.html"
class GisListPost(ListView):
model = Blog
queryset = Blog.objects.filter(category="G.I.S.")
context_object_name = 'posts'
template_name = "gis_list_post.html"
class SuMoSinglePost(DetailView):
model = Blog
queryset = Blog.objects.filter(category="Sustainable Mobility")
context_object_name = 'post'
template_name = "sumo_single_post.html"
class SuMoListPost(ListView):
model = Blog
queryset = Blog.objects.filter(category="Sustainable Mobility")
context_object_name = 'posts'
template_name = "sumo_list_post.html"
下面是urls.py及其四个路径:
from django.urls import include, path
from .views import GisSinglePost, GisListPost, SuMoListPost, SuMoSinglePost
urlpatterns = [
path("gis/", GisListPost.as_view(), name="gis_list_post"),
path("gis/<slug:slug>/", GisSinglePost.as_view(), name="gis_single_post"),
path("sustainable-mobility/", SuMoListPost.as_view(), name="sumo_list_post"),
path("sustainable-mobility/<slug:slug>/", SuMoSinglePost.as_view(), name="sumo_single_post"),
]
当我单击 GIS 类别的单个 post 时,它会毫无问题地显示相关详细信息。但是当我点击另一个类别的 post 时,它会向我显示:
Page not found (404) Request Method: GET Request URL:
http://127.0.0.1:8000/gis/erova-mobilita/ Raised by:
blog.views.GisSinglePost
No Articolo found matching the query
You're seeing this error because you have DEBUG = True in your Django
settings file. Change that to False, and Django will display a
standard 404 page.
这个问题困扰我好几天了。 我该如何解决?
您应该重新定义您的 get_absolute_url
方法。由于类别为 G.I.S 的博客实例存在,如果类别为可持续移动的博客实例,您永远不会达到第二。
def get_absolute_url(self):
if self.category == "G.I.S.":
return reverse("gis_single_post", kwargs={"slug": self.slug})
elif self.category == "Sustainable Mobility":
return reverse("sumo_single_post", kwargs={"slug": self.slug})
尝试用下一种方式重新定义您的 get_absolute_url(self):
def get_absolute_url(self):
if self.category == "G.I.S.":
return reverse("gis_single_post", kwargs={"slug": self.slug})
if self.category == "Sustainable Mobility":
return reverse("sumo_single_post", kwargs={"slug": self.slug})
我正在开发我的个人博客;它只有两个类别,我想为这两个类别提供一个特定的 post 列表。
出于这个原因,我展开了 get_absolute_url,如下所示:
from django.db import models
from django.urls import reverse
CATEGORY_CHOICES = (
('G.I.S.', 'G.I.S.'),
('Sustainable Mobility', 'Sustainable Mobility'),
)
class Blog(models.Model):
"""
Blog's post definition
"""
title = models.CharField(
max_length=70,
unique=True,
)
slug = models.SlugField(
unique=True,
)
contents = models.TextField()
publishing_date = models.DateTimeField()
category = models.CharField(
max_length=50,
choices=CATEGORY_CHOICES,
)
def __str__(self):
return self.title
def get_absolute_url(self):
if Blog.objects.filter(category="G.I.S."):
return reverse("gis_single_post", kwargs={"slug": self.slug})
if Blog.objects.filter(category="Sustainable Mobility"):
return reverse("sumo_single_post", kwargs={"slug": self.slug})
下面可以看到views.py;它具有基于类别的不同模型:
from django.shortcuts import render
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import Blog
class GisSinglePost(DetailView):
model = Blog
queryset = Blog.objects.filter(category="G.I.S.")
context_object_name = 'post'
template_name = "gis_single_post.html"
class GisListPost(ListView):
model = Blog
queryset = Blog.objects.filter(category="G.I.S.")
context_object_name = 'posts'
template_name = "gis_list_post.html"
class SuMoSinglePost(DetailView):
model = Blog
queryset = Blog.objects.filter(category="Sustainable Mobility")
context_object_name = 'post'
template_name = "sumo_single_post.html"
class SuMoListPost(ListView):
model = Blog
queryset = Blog.objects.filter(category="Sustainable Mobility")
context_object_name = 'posts'
template_name = "sumo_list_post.html"
下面是urls.py及其四个路径:
from django.urls import include, path
from .views import GisSinglePost, GisListPost, SuMoListPost, SuMoSinglePost
urlpatterns = [
path("gis/", GisListPost.as_view(), name="gis_list_post"),
path("gis/<slug:slug>/", GisSinglePost.as_view(), name="gis_single_post"),
path("sustainable-mobility/", SuMoListPost.as_view(), name="sumo_list_post"),
path("sustainable-mobility/<slug:slug>/", SuMoSinglePost.as_view(), name="sumo_single_post"),
]
当我单击 GIS 类别的单个 post 时,它会毫无问题地显示相关详细信息。但是当我点击另一个类别的 post 时,它会向我显示:
Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/gis/erova-mobilita/ Raised by: blog.views.GisSinglePost
No Articolo found matching the query
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
这个问题困扰我好几天了。 我该如何解决?
您应该重新定义您的 get_absolute_url
方法。由于类别为 G.I.S 的博客实例存在,如果类别为可持续移动的博客实例,您永远不会达到第二。
def get_absolute_url(self):
if self.category == "G.I.S.":
return reverse("gis_single_post", kwargs={"slug": self.slug})
elif self.category == "Sustainable Mobility":
return reverse("sumo_single_post", kwargs={"slug": self.slug})
尝试用下一种方式重新定义您的 get_absolute_url(self):
def get_absolute_url(self):
if self.category == "G.I.S.":
return reverse("gis_single_post", kwargs={"slug": self.slug})
if self.category == "Sustainable Mobility":
return reverse("sumo_single_post", kwargs={"slug": self.slug})