我的博客有发帖子的能力。我想要一个可以更新/编辑博客的功能,当我尝试实现该功能时,我遇到了以下错误;
NoReverseMatch at /post/1/ 未找到带有参数 '('',)' 的 'post_edit' 反向匹配。尝试了 1 个模式:['post/(?P[0-9]+)/edit/$']
我知道哪一行导致了问题:
/post_detail.html
<a href="{% url 'post_edit' post.pk %}"> +Edit Blog Post</a>
Run Code Online (Sandbox Code Playgroud)
如果没有上面的线,我就不会收到任何错误。我只是一个学习 Django 的初学者,我无法理解为什么这不起作用。我正在遵循的教程中建议了这一点。
/urls.py
urlpatterns = [
path('post/<int:pk>/edit/', BlogUpdateView.as_view(), name='post_edit'), # new
path('post/new/', BlogCreateView.as_view(), name='post_new'),
path('post/<int:pk>/', BlogDetailView.as_view(), name='post_detail'),
path('', BlogListView.as_view(), name='home'),
]
Run Code Online (Sandbox Code Playgroud)
/post_detail.html
{% extends 'base.html' %}
{% block content %}
<div class="post-entry">
<h2>
{{ my_posts.title }}
</h2>
<p>
{{ my_posts.body }}
</p>
</div>
<a href="{% url 'post_edit' post.pk %}"> +Edit Blog Post</a>
{% endblock content %}
Run Code Online (Sandbox Code Playgroud)
视图.py …