Python NoReverseMatch at/blog/-Django示例代码列表和详细视图问题

Python NoReverseMatch at/blog/-Django示例代码列表和详细视图问题,python,django,Python,Django,`我正在通过示例编写django第1章中的代码。遵循所有指示,并审查了该网站和其他领域的许多反馈,但我运气不好。我之前尝试过获取绝对url并返回反向方法,但没有成功。我尝试了一种有指导的方法,在listview和detailview中遇到了完全相同的问题。我是一个初学者,所以我想我错过了一些基本的东西。是否存在版本依赖关系。我安装了最新的django和python。需要建议,谢谢你的帮助! models.py views.py 从django.shortcuts导入渲染,获取\u object

`我正在通过示例编写django第1章中的代码。遵循所有指示,并审查了该网站和其他领域的许多反馈,但我运气不好。我之前尝试过获取绝对url并返回反向方法,但没有成功。我尝试了一种有指导的方法,在listview和detailview中遇到了完全相同的问题。我是一个初学者,所以我想我错过了一些基本的东西。是否存在版本依赖关系。我安装了最新的django和python。需要建议,谢谢你的帮助!

models.py views.py 从django.shortcuts导入渲染,获取\u object\u或\u 404 从。模型导入后

def post_list(request):
    posts = Post.published.all()
    return render(request, 'blog/post/post_list.html', {'posts':posts})

def post_detail(request, year, month, day, post):
    post = get_object_or_404(Post, slug=post,
                                    status='published',
                                    publish_date__year=year,
                                    publish_date__month=month,
                                    publish_date__day=day)

    return render(request, 'blog/post/post_detail.html',{'post':post})
url.py(用于应用程序) post_detail.html
{%extends“blog/base.html”%}
{%block title%}{{post.title}{%endblock%}
{%block content%}
{{post.title}

由{post.author}出版{post.publish_date}}

{{post.body | linebreaks}} {%endblock%}
post_list.html
{%extends“blog/base.html”%}
{%block title%}我的博客{%endblock%}
{%block content%}
我的博客
{posts%%中的post为%s}

由{post.author}出版{post.publish_date}}

{{post.body | truncatewords:30 | linebreaks}} {%endfor%} {%endblock%}
在您的post\u list.html中

改变这个

<a href="{% url 'blog:post_list' post.title %}"

谢谢Sachin,明白了。列表视图起作用了!但细节视图没有响应。再次丢失某些内容如果您将路径更改为
///
///
,如果它不起作用,请发送错误消息谢谢您的疏忽是有用的,我使用了,现在这些东西起作用了。但是,如果我必须通过传递参数来使用修改后的代码post_列表。我的猜测是,我没有传递要呈现的参数。如果我使用这种方法,你能告诉我代码中缺少什么吗。非常感谢你的建议!使用{%url'viewname'args%},这是访问url的最佳方式,请您对答案进行投票,如果有用,谢谢Hanks Sachin,我也应用了同样的方法。可能是我的论点定义不正确。您可以查看修改后的post_list.html。我试图提高投票率,但没有,它说只有不到15个声誉。这是我的第一篇文章。无论如何,非常感谢
from django.urls import path
from . import views

app_name = 'blog'

urlpatterns = [
#post views
    path('', views.post_list, name='post_list'),
    path('<int:year>/<int:month>/<int=day>/<slug=post>/',
        views.post_detail,
        name='post_detail'),
]
from django.contrib import admin

from .models import Post

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display = ('title', 'slug', 'author', 'publish_date', 'status')
    list_filter = ('status', 'created_date','publish_date', 'author')
    search_fields = ('title', 'body')
    prepopulated_fields = {'slug':('title',)}
    raw_id_fields = ('author',)
    date_hierarchy = 'publish_date'
    ordering = ('status', 'publish_date')
{% extends "blog/base.html" %}

{% block title %}{{post.title}}{% endblock %}

{% block content %}
<h> {{post.title}} </h>
  <p class="date">
    Published {{ post.publish_date }} by {{ post.author }}
  </p>
  {{ post.body|linebreaks }}
{%endblock%}
{% extends "blog/base.html" %}

{% block title %}My Blog{% endblock %}

{% block content %}
  <h1>My Blog</h1>
  {% for post in posts %}
    <h2>
        <a href="{% url 'blog:post_list' post.title %}"
          {{ post.title }}
        </a>
    </h2>
    <p class="date">
        Published {{ post.publish_date }} by {{ post.author }}
    </p>
        {{ post.body|truncatewords:30|linebreaks }}
  {% endfor %}
{% endblock %}
{% extends "blog/base.html" %}

{% block title %}My Blog{% endblock %}

{% block content %}
  <h1>My Blog</h1>
  {% for post in posts %}
    <h2>
          {% url 'blog:post_detail' post.publish_date.year post.publish_date.month post.publish_date.day post.publish_date.slug as post.title %}
          <a href="{{ post.title }}">
          {{ post.title }}
          </a>
    </h2>
    <p class="date">
        Published {{ post.publish_date }} by {{ post.author }}
    </p>
        {{ post.body|truncatewords:30|linebreaks }}
  {% endfor %}
{% endblock %}
<a href="{% url 'blog:post_list' post.title %}"
<a href="{% url 'blog:post_list'%}"