post_编辑URL上的Django NoReverseMatch

post_编辑URL上的Django NoReverseMatch,django,django-forms,django-templates,django-views,Django,Django Forms,Django Templates,Django Views,我在我的博客中添加了一个编辑视图,这样我的助手就可以从前端而不是管理区进行编辑。我的post\u editURL设置与我的post\u detail相同,只是末尾有一个/edit/属性。当我在查看一篇文章时,我手动将/edit/添加到URL的末尾,效果很好,但是我遇到了一个问题,创建了一个编辑按钮并传递了参数 这是浏览器错误: NoReverseMatch at/press/2016/05/23/gdfgdfcdcd/ 未找到带参数(2016,5,23,'gdfgdfccd')和关键字参数“{}

我在我的博客中添加了一个编辑视图,这样我的助手就可以从前端而不是管理区进行编辑。我的
post\u edit
URL设置与我的
post\u detail
相同,只是末尾有一个
/edit/
属性。当我在查看一篇文章时,我手动将
/edit/
添加到URL的末尾,效果很好,但是我遇到了一个问题,创建了一个编辑按钮并传递了参数

这是浏览器错误:

NoReverseMatch at/press/2016/05/23/gdfgdfcdcd/ 未找到带参数(2016,5,23,'gdfgdfccd')和关键字参数“{}”的“post_edit”的反转。尝试了1个模式:[按/(?P\d{4})/(?P\d{2})/(?P\d{2})/(?P[-\w]+)/edit/$]

谢谢你的关注

url

urlpatterns = [
    ...
    url(r'^press/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>[-\w]+)/$', views.post_detail, name='post_detail'),
    url(r'^press/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>[-\w]+)/edit/$', views.post_edit, name='post_edit'),
    ...
]
模板

<a href="{% url 'press:post_edit' post.created.year post.created.month post.created.day post.slug %}"><i class="fa fa-envelope-o" aria-hidden="true"></i> Edit Post</a>

在URL中,您确实为day和month参数添加了{2},这意味着您需要每个参数都是两个十进制字符才能生效,这是不正确的,因此最好将其更改为{1,2}:

urlpatterns = [
    ...
    url(r'^press/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<post>[-\w]+)/$', views.post_detail, name='post_detail'),
    url(r'^press/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<post>[-\w]+)/edit/$', views.post_edit, name='post_edit'),
    ...
]
urlpatterns=[
...
url(r'^press/(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})/(?P[-\w]+)/$”,views.post_detail,name='post_detail'),
url(r'^press/(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})/(?P[-\w]+)/edit/$”,views.post_edit,name='post_edit'),
...
]

在URL中,您确实为day和month参数添加了{2},这意味着您需要每个参数都是两个十进制字符才能生效,这是不正确的,因此最好将其更改为{1,2}:

urlpatterns = [
    ...
    url(r'^press/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<post>[-\w]+)/$', views.post_detail, name='post_detail'),
    url(r'^press/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<post>[-\w]+)/edit/$', views.post_edit, name='post_edit'),
    ...
]
urlpatterns=[
...
url(r'^press/(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})/(?P[-\w]+)/$”,views.post_detail,name='post_detail'),
url(r'^press/(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})/(?P[-\w]+)/edit/$”,views.post_edit,name='post_edit'),
...
]

您的正则表达式不匹配,因为它要求当月正好有2位数字,但您只传递了一位('5')。您应该确保月和日参数都接受一位或两位数字

r'^press/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})...
r'^press/(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})。。。

您的正则表达式不匹配,因为它要求当月正好有2位数字,但您只传递了一位('5')。您应该确保月和日参数都接受一位或两位数字

r'^press/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})...
r'^press/(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})。。。

错误消息显示正在使用的
post\u edit
模式……我假设它在末尾显示
/edit/
。我只包括了两个具有参数的url模式。错误消息显示正在使用的
post\u edit
模式…我假设它在末尾显示了
/edit/
。我包括了仅有的两个具有参数的url模式。