简体   繁体   English

NoReverseMatch at / Reverse for 'post_detail' 未找到

[英]NoReverseMatch at / Reverse for 'post_detail' not found

I'm developing a simple blog with Python and Django.我正在用 Python 和 Django 开发一个简单的博客。 On my home page, I display the 3 latest posts and then all the posts.在我的主页上,我显示 3 个最新帖子,然后是所有帖子。 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. NoReverseMatch at / Reverse for 'post_detail' 关键字参数为 '{'pk': ''}' 未找到。 1 pattern(s) tried: ['post/(?P[0-9]+)/$']尝试了 1 个模式:['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 :) PS:我正在关注这个法语教程(这个是英文的,但这里的英文有点不同。请注意,我只是开始使用 Python 和 Django :)

So here is my code:所以这是我的代码:

post_list.html (template) post_list.html(模板)

{% 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 {% url 'post_detail' pk=post.pk %} ,在模板上

from django.shortcuts import render, get_object_or_404 , on views从 django.shortcuts 导入渲染,get_object_or_404 ,在视图上

**def post_detail(request, pk): **定义 post_detail(请求,pk):

post = get_object_or_404(Post, pk=pk) post = get_object_or_404(post, pk=pk)

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

url(r'^post/(?P[0-9]+)/$', views.post_detail, name='post_detail'), on urls url(r'^post/(?P[0-9]+)/$', views.post_detail, name='post_detail'),在 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:但是,第一次出现是在之前{% for post in posts %}循环,在那一刻有没有post变,也不过是一个latest变化(因为它位于{% for latest in latests %}循环)所以你可能想把它替换为:

<p class="showmore"><a href="{% url 'post_detail' pk=latest.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如果它是一个 heroku web 应用程序,运行 heroku run python manage.py createsuperuser 然后添加一个帖子,如果它是一个本地项目,只需运行 python manage.py createsuperuser 并添加一个帖子

  • 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.我在创建我的博客后遇到了同样的问题,但没有添加任何帖子,所以我在 / Reverse for 'post_detail' 中使用了这个NoReverseMatch,关键字参数为 '{'pk': ''}' not found。 1 pattern(s) tried: ['post/(?P[0-9]+)/$'] -尝试了 1 个模式:['post/(?P[0-9]+)/$'] -

just go to your admin and add those things只需转到您的管理员并添加这些内容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoReverseMatch at /post/new Reverse for &#39;post_detail&#39; 未找到。 “post_detail”不是有效的视图函数或模式名称 - NoReverseMatch at /post/new Reverse for 'post_detail' not found. 'post_detail' is not a valid view function or pattern name 如何修复 NoReverseMatch at / Reverse for &#39;post_detail&#39; 关键字参数 &#39;{u&#39;pk&#39;: &#39;&#39;}&#39; not found。 尝试了 1 个模式:[&#39;post/<int:pk> /&#39;] - How to fix NoReverseMatch at / Reverse for 'post_detail' with keyword arguments '{u'pk': ''}' not found. 1 pattern(s) tried: ['post/<int:pk>/'] 未找到“post_detail”的反转。 “post_detail”不是有效的视图函数或模式名称 - Reverse for 'post_detail' not found. 'post_detail' is not a valid view function or pattern name 未找到带有参数 &#39;(&#39;&#39;,)&#39; 的 &#39;post_detail&#39; 反转。 尝试了 1 个模式:[&#39;post/(?P<slug> [-\\\\w]+)/$&#39;] - Reverse for 'post_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['post/(?P<slug>[-\\w]+)/$'] 反向使用关键字 arguments '{'pk': '', 'article_id': ''}' not found - Reverse for 'post_detail' with keyword arguments '{'pk': '', 'article_id': ''}' not found Django 2.3 在 get_absolute_url 中,找不到“post_detail”的反转 - Django 2.3 Inside get_absolute_url, Reverse for 'post_detail' not found 使用参数 &#39;(&#39;&#39;,)&#39; 和关键字参数 &#39;{}&#39; 未找到的 &#39;post_detail&#39; 反转。 尝试了 1 个模式:[&#39;posts/(?P[0-9]+)/$&#39;] - Reverse for 'post_detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['posts/(?P[0-9]+)/$'] 如何解决此问题: 使用关键字 arguments '{'slug': ''}' 反向查找 'post_detail'。 尝试了 1 种模式:['(?P<slug> [-a-zA-Z0-9_]+)/$']?</slug> - How to fix this: Reverse for 'post_detail' with keyword arguments '{'slug': ''}' not found. 1 pattern(s) tried: ['(?P<slug>[-a-zA-Z0-9_]+)/$']? 为什么我在错误处得到 NoReverseMatch // 反转“post-detail”,参数为“(&#39;&#39;,)”未找到 - Why I Get NoReverseMatch at Error // Reverse for 'post-detail' with arguments '('',)' not found NoReverseMatch at /signup/ - 反转为 &#39;<WSGIRequest: POST '/signup/'> &#39; 未找到 - NoReverseMatch at /signup/ - Reverse for '<WSGIRequest: POST '/signup/'>' not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM