Django Error 3.1.3 Reverse for 'post_detail' not found. 'post_detail' is not a Valid view Function or Pattern name

Asked

Viewed 200 times

0

Python 3.8.3 Django 3.1.3

I don’t understand why this mistake is happening.

Reverse for 'post_detail' not found. 'post_detail' is not a Valid view Function or Pattern name.

The line the error accuses is:

<li><a href="{% url 'post_detail' post.id %}" class="button">Read More</a></li>

The function is created in views.py:

blog/blog/views.py

from django.shortcuts import render
from website.models import Post

def post_detail(request, id):
    post = Post.objects.get(id=id)
    return render(request, 'post_detail.html', {'post': post})

Urls imports the post_detail module, and uses it to create a new link according to the id of each post.

website/urls.py

from django.urls import path, include
from .views import hello_blog, post_detail

urlpatterns = [
    path('', hello_blog),
    path('post/<int:id>/', post_detail, name='post_detail'),
]

blog/website/templates/post_detail.html

<h1>Meu Post</h1>
{{ post }}

The Code works perfectly well, until I enter the command line to redirect the link: blog/website/templates/index.html

<html>
                    <section id="two">
                        <h2>Recent Work</h2>
                        <div class="row">
                            {% for post in posts %}
                            <article class="col-6 col-12-xsmall work-item">
                                <a href="{% static 'images/fulls/01.jpg' %}" class="image fit thumb"><img src="{% static 'images/thumbs/01.jpg' %}" alt="" /></a>
                                <h3>{{ post.title }}</h3>
                                <p>{{ post.sub_title }}</p>
                                <span> {{ post.get_category_label }} </span>
                                
                                    <ul class="actions">
                                        <li><a href="{% url 'post_detail' post.id %}" class="button">Read More</a></li>
                                    </ul>
                                
                            </article>
                            {% endfor %}
                        </div>
</html>

As soon as I insert the line:Read More

1 answer

1

Caro Vitor,

I believe the way to solve the problem is to add the name of the app in the file urls.py, leaving him as per below:

from django.urls import path, include
from .views import hello_blog, post_detail

app_name = 'minha_app'

urlpatterns = [
    path('', hello_blog),
    path('post/<int:id>/', post_detail, name='post_detail'),
]

Then just enter the name of the app in the template as below.

<li><a href="{% url 'minha_app:post_detail' post.id %}" class="button">Read More</a></li>

This suggested form prevents Django from getting confused with the name used post_detail since it is the same name of the method within views.py.

It seems strange, but a template can call a method directly, but Django prevents the passing of parameters in this case. See here

I believe it is also necessary to change the name of the variable in urls.py and explain it in the template when you pass the url, as below.

urls.py

path('post/<int:post_id>/', post_detail, name='post_detail'),

blog/website/templates/index.html

<li><a href="{% url 'minha_app:post_detail' post_id=post.id %}" class="button">Read More</a></li>

I hope it helps

Browser other questions tagged

You are not signed in. Login or sign up in order to post.