You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django3.2.7 Wagtail项目NoReverseMatch错误:找不到latest_posts路由

问题原因

错误触发的根本原因是 blog_listing_page.html 模板中调用可路由页面URL时,路由名称字符串带了多余的前置空格:
你当前的代码为:

<a href="{% routablepageurl page " latest_posts" %}">View Latest Posts Only</a>

Django/Wagtail的路由名称是严格字符串匹配,你定义的路由名称是无空格的 latest_posts,模板中传递的是带前置空格的 latest_posts,因此无法匹配到对应路由触发异常。

解决方案

  1. 删除模板中路由名称前的多余空格,修改为:
<a href="{% routablepageurl page "latest_posts" %}">View Latest Posts Only</a>
  1. 额外确认你的博客列表页模型代码中,可路由方法的装饰器参数正确,示例如下:
from wagtail.contrib.routable_page.models import RoutablePageMixin, route

class BlogListingPage(RoutablePageMixin, Page):
    # 此处省略你的其他字段定义
    @route(r"^latest/$", name="latest_posts")
    def latest_posts(self, request):
        # 此处省略获取最新文章的逻辑
        return self.render(request, template="latest_posts.html", context={"posts": 最新文章查询集})

修改完成后重启开发服务即可正常访问。

内容的提问来源于stack exchange,提问作者pedro henrique garcia pinto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 12:57:02