简体   繁体   中英

NoReverseMatch at / Reverse for 'post_detail' not found

I'm developing a simple blog with Python and Django. On my home page, I display the 3 latest posts and then all the posts. At this point my blog is working. But I added a link "Show more" or "Show article" to display the post alone in a new page but I get the following error when I load the page:

NoReverseMatch at / Reverse for 'post_detail' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['post/(?P[0-9]+)/$']

PS: I'm following this tutorial in french (this one is in English but it is a bit different in english here . And please note that I'm juste starting Python and Django :)

So here is my code:

post_list.html (template)

{% extends 'blog/base.html' %}

{% block latestsnews %}
    {% for latest in latests %}
        <article class="lastnews">
            <h4>{{ latest.title }}</h4>
            <h5 class="lastestcategory">{{ latest.category }}</h5>
            <p class="bodysmall">{{ latest.text|truncatewords:10 }}</p>
            <div>
                <p class="date">{{ latest.published_date }}</p>
                <p class="showmore"><a href="{% url 'post_detail' pk=post.pk %}">Show more</a></p>
            </div>
        </article>
    {% endfor %}
{% endblock %}

{% block posts %}
    {% for post in posts %}
    <article class="post">
        <header class="postheader">
            <h4>{{ post.title }}</h4>
            <p class="info">{{ post.category }}, {{ post.published_date }}</p>
        </header>
        <p class="bodyregular">{{ post.text|linebreaksbr }}</p>
        <footer class="postfooter">
            <p class="author">Author: {{ post.author }}</p>
            <p class="showarticle"><a href="{% url 'post_detail' pk=post.pk %}">Show article</a></p>
        </footer>
    </article>
    {% endfor %}
{% endblock %}

Views

from django.shortcuts import render
from django.utils import timezone
from .models import Category, Post
from django.shortcuts import render, get_object_or_404

def category_list(request):
    categories = Category.objects.all()
    return render (request, 'blog/post_list.html', {'categories': categories})

def post_list(request):
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    latests = Post.objects.filter(published_date__lte=timezone.now()).reverse()[:3]
    return render(request, 'blog/post_list.html', {'posts': posts, 'latests': latests})

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})

Urls

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.post_list, name='post_list'),
    url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
]

Code that I added since it's not working

{% url 'post_detail' pk=post.pk %} , on template

from django.shortcuts import render, get_object_or_404 , on views

**def post_detail(request, pk):

post = get_object_or_404(Post, pk=pk)

return render(request, 'blog/post_detail.html', {'post': post})**, on views

url(r'^post/(?P[0-9]+)/$', views.post_detail, name='post_detail'), on urls

Thanks in advance !

In your template you write:

{% for latest in latests %}
<!-- ... -->
<p class="showmore"><a href="{% url 'post_detail' pk=post.pk %}">Show more</a></p>
<!-- ... -->
{% endfor %}

But the first occurance is before the {% for post in posts %} loop, at that moment there is no post variable, there is however a latest variable (since it is located in the {% for latest in latests %} loop), so you might want to replace it with:

<p class="showmore"><a href="{% url 'post_detail' pk=.pk %}">Show more</a></p>

if its a heroku web app, run heroku run python manage.py createsuperuser then add a post, if its a local project just run python manage.py createsuperuser and add a post

  • i had the same issue after creating my blog but didn't add any post so i had this NoReverseMatch at / Reverse for 'post_detail' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['post/(?P[0-9]+)/$'] -

just go to your admin and add those things

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM