我正在开发自己的个人博客;它只有两个类别,我想为这两个类别提供特定的帖子列表。
因此,我将展开 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类别的单个帖子时,就会显示相对详细信息,而不会出现问题。但是,当我单击其他类别的帖子时,会显示以下内容:
找不到页面(404)请求方法:GET请求URL: http://127.0.0.1:8000/gis/erova-mobilita/提出者: blog.views.GisSinglePost
找不到与查询匹配的Articolo
您看到此错误,因为您的Django中的DEBUG = True 设置文件。将其更改为False,Django将显示一个 标准404页面。
我已经在这个问题上困扰了很多天。 我该如何解决?
答案 0 :(得分:2)
您应该重新定义get_absolute_url
方法。由于您的类别为G.I.S的Blog实例存在,因此对于类别为Sustainable Mobility的Blog实例,您再也不会排名第二。
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})
答案 1 :(得分:1)
尝试通过以下方式重新定义您的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})