反向查找“ detailed_view”,未找到任何参数

时间:2018-11-10 20:05:25

标签: python django django-templates django-views django-urls

以下是我来自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'的错误反向,但没有找到参数。

能请您解释和纠正。谢谢

杰夫

1 个答案:

答案 0 :(得分:0)

Change the line in html to :

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