首页 > 解决方案 > 为什么我在错误时得到 NoReverseMatch // 未找到带有参数 '('',)' 的 'post-detail' 的反向

问题描述

我找不到错误的原因。

网址.py:

path('', views.post_list, name="post_list"),
path('<str:url_sistem>/', views.post_detail, name='post-detail'),

视图.py:

def post_detail(request, url_sistem):
url_sistem = Post.objects.get(title)
posts = get_object_or_404(Post, url_sistem=url_sistem)
return render(request, 'blog/post_detail.html', {'posts':posts})

一个href链接:

<a href="{% url 'post-detail' post.url_sistem %}" style="color:black;">

标签: pythondjangodjango-viewsdjango-templates

解决方案


您有一个需要修复的缩进错误:

def post_detail(request, url_sistem):
    url_sistem = Post.objects.get(title)
    posts = get_object_or_404(Post, url_sistem=url_sistem)
    return render(request, 'blog/post_detail.html', {'posts':posts})

在模板中:

<a href="{% url 'blog:post-detail' post.url_sistem %}" style="color:black;">

推荐阅读