我有这个 post_list.html 文件:(忽略第二个 'blog:post_detail' 的 URL)
当我运行服务器时,出现了无法找到反向匹配/的错误:
然而,如果我将re_path替换为url,将
{% for post in post_list %}
<h1><a href="{% url 'blog:post_detail' post.pk %}">{{ post.title }}</a></h1>
<div class="date">
<p>Published on: {{post.published|date:"D M Y"}}</p>
</div>
<a href="{url 'blog:post_detail' post.pk}">Comments: {{post.approve_comments.count}}</a>
{% endfor %}
在 urls.py 文件中,我正在尝试以以下方式使用 re_path:
re_path(r'^posts/<int:pk>/$', views.PostDetailView.as_view(), name='post_detail'),
当我运行服务器时,出现了无法找到反向匹配/的错误:
尝试使用模式“posts/<int:pk>/$”,但未发现参数为“(1,)” 的“post_detail”反向匹配。
然而,如果我将re_path替换为url,将
<int:pk>
替换为(?P<pk>\d+)
,它就可以完美地工作:url(r'^posts/(?P<pk>\d+)/$', views.PostDetailView.as_view(), name='post_detail'),