Home > Mobile >  Django 3.2.7 - NoReverseMatch at /blog/ Reverse for ' latest_posts' not found. ' late
Django 3.2.7 - NoReverseMatch at /blog/ Reverse for ' latest_posts' not found. ' late

Time:10-16

While following a tutorial from "Coding For Everybody - Learn wagtail", i ran into a problem while messing with the Routable Pages video, which i copypasted the code found at his GitHub into my Blog and now the following error appears:

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

My 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 %}

my 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/[email protected]/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/[email protected]/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>

My 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 %}

I've tried to redo all the steps, watched the video over and over but nothing worked.

CodePudding user response:

In the line

{% routablepageurl page " latest_posts" %}

you have a leading space before latest_posts - you should remove this.

CodePudding user response:

The error message itself indicate that the url namespace you are looking for does not exist ' latest_posts'

Look here :-

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

So if you check in your urls.py file you will find the correct namespace, that you can use at that place. As of now the issue is in the My blog_listing_page.html file.

  • Related