找不到如何使用关键字参数'{u'pk':''}')将'post_detail'固定为/ Reverse的NoReverseMatch。尝试了1个模式:['post / <int:pk> /']

时间:2019-04-08 16:19:41

标签: python django

我已经尝试了在互联网上发现的所有与我的问题类似的东西,但没有帮助。如果您知道答案,请提供帮助。

尝试设置博客的post_detail页面时收到错误消息。

错误消息:

Reverse for 'post_detail' with keyword arguments '{u'pk': ''}' not found. 
1 pattern(s) tried: ['post/<int:pk>/']

我的post_list.html

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

{% load static %}

<html>
    <head>
        <title>Code Reminder</title>
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
        <link rel="stylesheet" href="{% static 'css/blog.css' %}">
        <link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
    </head>
    <body>
        <div class="page-header">
            <h1><a href="/">Coder Reminder</a></h1>
        </div>

<div class="content container">
    <div class="row">
        <div class="col-md-4">

{% block content %}
    {% for post in posts %}
        <div class="post">
            <div class="date">
                {{ post.published_date }}
            </div>
            <h1><a href="{% url 'post_detail' pk=post.blog.pk %}">{{ post.title }}</a></h1>
            <p>{{ post.text|linebreaksbr }}</p>
        </div>
    {% endfor %}
{% endblock %}

        </div>
    </div>
</div>

    </body>
</html>

我的post_detail.html

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

{% block content %}
    <div class="post">
        {% if post.published_date %}
            <div class="date">
                {{ post.published_date }}
            </div>
        {% endif %}
        <h2>{{ post.title }}</h2>
        <p>{{ post.text|linebreaksbr }}</p>
    </div>
{% endblock %}

我的base.html

{% load static %}

<html>
    <head>
        <title>Code Reminder</title>
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
        <link rel="stylesheet" href="{% static 'css/blog.css' %}">
        <link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
    </head>

    <body>
    <div class="page-header">
        <h1><a href="/">Code Reminder</a></h1>
    </div>
    <div class="content container">
        <div class="row">
            <div class="col-md-8">
            {% block content %}
            {% endblock %}
            </div>
        </div>
    </div>
</body>

</html>

我的views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

# Create your views here.
from django.shortcuts import render
from django.utils import timezone
from .models import Post
from django.shortcuts import render, get_object_or_404


def post_list(request):
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, '/home/ud/PycharmProjects/blog/blog/blog/templates/blog/post_list.html', {'posts': posts})


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

我的urls.py

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

urlpatterns = [
    url('', views.post_list, name='post_list'),
    url('post/<int:pk>/', views.post_detail, name='post_detail'),
]

最后是我的myblog / urls.py

from django.conf.urls import url
from django.conf.urls import include
from django.contrib import admin


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url('', include('blog.urls')),
]

如果您知道答案,请帮助我。我真的尽了一切,但我找不到错误。

致谢

2 个答案:

答案 0 :(得分:2)

错误消息说

Reverse for 'post_detail' with keyword arguments '{u'pk': ''}' not found. 

这表明pk的计算结果为空字符串''

您的URL标记是

{% url 'post_detail' pk=post.blog.pk %}

这意味着您正在尝试传递该帖子的博客pk。但是您几乎可以肯定地想要该帖子的pk:

{% url 'post_detail' pk=post.pk %}

第二,您正在使用具有url()语法的旧path()(在Django 2.0中是新语法)。您的网址应为:

from django.urls import path

urlpatterns = [
    path('', views.post_list, name='post_list'),
    path('post/<int:pk>/', views.post_detail, name='post_detail'),
]

答案 1 :(得分:0)

问题可能是由于Python或Django的版本较旧。 尝试如下更改您的我的urls.py 文件:

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

此处将有助于正确解析地址。如果仍然无法正常运行,请尝试替换我的post_list.html 文件

中的以下内容

旧:

{% url 'post_detail' pk=post.blog.pk %}

新功能:

{% url 'post_detail' pk=post.pk %}

希望这会有所帮助!