首页 > 解决方案 > 未找到没有参数的“detailed_view”反向

问题描述

以下是来自views.py 的代码

from . import views

app_name = 'blogs'

urlpatterns = [
   path('', views.blogs_home, name='blogs'),
   path('<int:post_id>', views.single_blog, name='detailed_view'),
]

以下代码来自views.py

def single_blog(request,post_id):
   blog_single = Blogs.objects.get(id=post_id)
   context = {'blog_single': blog_single}
   template = 'blog_home.html'

   return render(request, template, context)

下面给出了html

{% for b in blogs %}
<div>
    <h1><a href="{% url 'blogs:detailed_view' id=b.id %}">{{ b.title }}</a></h1>
    <p>{{ b.body }}</p>
    <aside>{{ b.date }}</aside>
</div>
{% endfor %}

但是,它会返回“detailed_view”的错误反向,没有找不到参数。

能否请您解释并纠正。谢谢

杰夫

标签: pythondjangodjango-templatesdjango-viewsdjango-urls

解决方案


将 html 中的行更改为:

<h1><a href="{% url 'blogs:detailed_view' post_id=b.id %}">{{ b.title }}</a></h1>

推荐阅读