Django 3.2.7 - 在 /blog/ Reverse 找不到“latest_posts”的 NoReverseMatch。 “latest_posts”不是有效的视图函数或模式名称

Django 3.2.7 - NoReverseMatch at /blog/ Reverse for ' latest_posts' not found. ' latest_posts' is not a valid view function or pattern name

在学习“Coding For Everybody - Learn wagtail”的教程时,我 运行 在搞砸时遇到了一个问题使用可路由页面视频,我将在他的 GitHub 中找到的代码复制粘贴到我的博客中,现在出现以下错误:

Reverse for ' latest_posts' not found. ' latest_posts' is not a valid view function or pattern name.
Request Method: GET
Request URL:    http://localhost:8000/blog/
Django Version: 3.2.7
Exception Type: NoReverseMatch
Exception Value:    
Reverse for ' latest_posts' not found. ' latest_posts' is not a valid view function or pattern name.
Exception Location: C:\Users\pedro.garcia\AppData\Local\Programs\Python\Python39\lib\site-packages\django\urls\resolvers.py, line 694, in _reverse_with_prefix
Python Executable:  C:\Users\pedro.garcia\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.6
Python Path:    
['C:\Users\pedro.garcia\website\mysite',
 'C:\Users\pedro.garcia\AppData\Local\Programs\Python\Python39\python39.zip',
 'C:\Users\pedro.garcia\AppData\Local\Programs\Python\Python39\DLLs',
 'C:\Users\pedro.garcia\AppData\Local\Programs\Python\Python39\lib',
 'C:\Users\pedro.garcia\AppData\Local\Programs\Python\Python39',
 'C:\Users\pedro.garcia\AppData\Local\Programs\Python\Python39\lib\site-packages']
Server time:    Fri, 15 Oct 2021 13:28:15 +0000

我的latest_posts.html:

{% extends "base.html" %}

{% load wagtailimages_tags %}

{% block content %}

<div class="container">
    <h1>Latest Posts</h1>
    {% for post in posts %}
    <div class="row mt-5 mb-5">
        <div class="col-sm-3">
            {% image post.blog_image fill-250x250 as blog_img %}
            <a href="{{ post.url }}">
                <img src="{{ blog_img.url }}" alt="{{ blog_img.alt }}">
            </a>
        </div>
        <div class="col-sm-9">
            <a href="{{ post.url }}">
                <h2>{{ post.custom_title }}</h2>
                {# @todo add a summary field to BlogDetailPage; make it a RichTextField with only Bold and Italic
                enabled. #}
                <a href="{{ post.url }}" class="btn btn-primary mt-4">Read More</a>
            </a>
        </div>
    </div>
    {% endfor %}
</div>
{% endblock content %}

我的base.html:

{% load static wagtailuserbar %}

<!DOCTYPE html>
<html class="no-js" lang="en">
    <head>
        <meta charset="utf-8" />
        <title>
            {% block title %}
                {% if self.seo_title %}{{ self.seo_title }}{% else %}{{ self.title }}{% endif %}
            {% endblock %}
            {% block title_suffix %}
                {% with self.get_site.site_name as site_name %}
                    {% if site_name %}- {{ site_name }}{% endif %}
                {% endwith %}
            {% endblock %}
        </title>
        <meta name="description" content="" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />

        {# Global stylesheets #}
        <script src="https://kit.fontawesome.com/cf9856e86a.js" crossorigin="anonymous"></script>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
        <link rel="stylesheet" type="text/css" href="{% static 'css/mysite.css' %}">

        {% block extra_css %}
            {# Override this in templates to add extra stylesheets #}
        {% endblock %}
    </head>

    <body class="{% block body_class %}{% endblock %}">
        {% wagtailuserbar %}
        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">Sesacre</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarColor02" aria-controls="navbarColor02" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse" id="navbarColor02">
            
            <ul class="navbar-nav me-auto">
                <li class="nav-item">
                <a class="nav-link active" href="/">início
                    <span class="visually-hidden">(current)</span>
                </a>
                </li>
                <li class="nav-item">
                <a class="nav-link" href="/blog/">Blog</a>
                </li>
                </div>
                </li>
            </ul>
            <form class="d-flex">
                <input class="form-control me-sm-2" type="text" placeholder="Search">
                <button class="btn btn-secondary my-2 my-sm-0" type="submit">Pesquisa</button>
            </form>
        </div>
    </nav>
        {% block content %}{% endblock %}
            <div class="container">
                <div class="row">
                    <div class="col-lg-12 text-center">
                        {% if settings.site_settings.SocialMediaSettings.facebook %}
                        <a href="{{ settings.site_settings.SocialMediaSettings.facebook }}">
                            <i class="fab fa-facebook-f"></i>
                        </a>
                        {% endif %}
                        {% if settings.site_settings.SocialMediaSettings.twitter %}
                        <a href="{{ settings.site_settings.SocialMediaSettings.twitter }}">
                            <i class="fab fa-twitter"></i>
                        </a>
                        {% endif %}
                        {% if settings.site_settings.SocialMediaSettings.youtube %}
                        <a href="{{ settings.site_settings.SocialMediaSettings.youtube }}">
                            <i class="fab fa-youtube"></i>
                        </a>
                        {% endif %}
                    </div>
                </div>
            </div>
        {# Global javascript #}
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>
        <script type="text/javascript" src="{% static 'js/mysite.js' %}"></script>



        {% block extra_js %}
            {# Override this in templates to add extra javascript #}
        {% endblock %}
    </body>
</html>

我的blog_listing_page.html:

{% extends "base.html" %}
{% load wagtailimages_tags wagtailroutablepage_tags %}
{% block content %}

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

<h2>
    Categories:
    <small>
        {% for cat in categories %}
        <a href="?category={{ cat.slug }}">
            {{ cat.name }}
        </a>{% if not forloop.last %}, {% endif %}
        {% endfor %}
    </small>
</h2>

<div class="container">
    {% for post in posts %}
    <div class="row mt-5 mb-5">
        <div class="col-sm-3">
            {% image post.blog_image fill-250x250 as blog_img %}
            <a href="{{ post.url }}">
                <img src="{{ blog_img.url }}" alt="{{ blog_img.alt }}">
            </a>
        </div>
        <div class="col-sm-9">
            <a href="{{ post.url }}">
                <h2>{{ post.custom_title }}</h2>
                {# @todo add a summary field to BlogDetailPage; make it a RichTextField with only Bold and Italic
                enabled. #}
                <a href="{{ post.url }}" class="btn btn-primary mt-4">Read More</a>
            </a>
        </div>
    </div>
    {% endfor %}
</div>
{% endblock content %}

我尝试重做所有步骤,反复观看视频,但没有任何效果。

在行

{% routablepageurl page " latest_posts" %}

你在 latest_posts 之前有一个前导 space - 你应该删除它。

错误消息本身表明您要查找的 url 命名空间不存在 ' latest_posts'

看这里:-

Reverse for ' latest_posts' not found. ' latest_posts' is not a valid view function or pattern name.

因此,如果您签入 urls.py 文件,您将找到可以在该位置使用的正确命名空间。 截至目前,问题在 My blog_listing_page.html 文件中。