【问题标题】:Incorrect redirection for a single post category单个帖子类别的重定向错误
【发布时间】:2019-04-14 08:11:59
【问题描述】:

我正在开发我的个人博客;它只有两个类别,我想为这两个类别提供一个特定的帖子列表。

出于这个原因,我扩展了 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 页面。

我已经被这个问题困扰了很多天。 我该如何解决

【问题讨论】:

    标签: django django-models django-views django-urls django-2.1


    【解决方案1】:

    您应该重新定义您的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})
    

    【讨论】:

    • 如何重新定义它?
    • 请查看编辑后的答案。好的,我看到有人直接回答了。当然不用解释为什么。有趣的方法。
    【解决方案2】:

    尝试用下一种方式重新定义你的 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})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      • 2020-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多