NoReverseMatch
来源:6-6 用户发表动态
慕容2094767
2020-09-18
老师,请帮忙看下这个问题
在发表动态的时候,点击发送之后报错。
django.urls.exceptions.NoReverseMatch: Reverse for ‘delete_news’ with arguments ‘(’’,)’ not found. 1 pattern(s) tried: [‘news/delete/(?P[^/]+)/$’]
HTTP POST /news/post-news/ 500 [1.95, 192.168.1.4:62539]
ERROR 2020-09-18 22:46:26,604 runserver 13802 139766025459456 HTTP POST /news/post-news/ 500 [1.95, 192.168.1.4:62539]
<AsgiRequest: POST ‘/news/post-news/’>
Internal Server Error: /news/post-news/
路由配置如下:
app_name = “news"
urlpatterns = [
path(”", views.NewsListView.as_view(), name=“list”),
path(‘post-news/’, views.post_news, name=‘post_news’),
path(‘delete//’, views.NewsDeleteView.as_view(), name=‘delete_news’),
path(‘like/’, views.like, name=‘like_post’),
path(‘get-thread/’, views.get_thread, name=‘get_thread’),
path(‘post-comment’, views.post_comment, name=‘post_comment’),
我点击的是动态提交的按钮,走的应该是post-news路由错误信息提示的是 delete_news,相当懵,甚至把 news_single.html中的a标签注释掉,还是没用,实在搞不懂提交动态跟 删除动态有啥关联。
views中的代码:
def post_news(request):
""“发送动态,AJAX POST 请求”""
post = request.POST[‘post’].strip()
print(request)
if post:
posted = News.objects. create(user=request.user, content=post)
#将request加到上下文渲染器中
html = render_to_string((‘news/news_single.html’, {‘news’:posted, ‘request’:request}))
return HttpResponse(html)
else:
return HttpResponseBadRequest(“内容不能为空”)
class NewsDeleteView(LoginRequiredMixin, AuthorRequireMixin, DeleteView):
""“删除动态,需要判断当前用户是否是原作者,通过自定义的AuthorRequireMixin类实现”""
model = News
template_name = ‘news/news_confirm_delete.html’
# slug_url_kwarg = ‘slug’ #通过url传入要删除的对象主键ID,默认值是pk
# pk_url_kwarg = ‘pk’#通过url传入要删除的对象主键ID,默认值是pk
success_url = reverse_lazy(“news:list”) #在项目URLConf未加载前使用
2回答
-
Jack
2020-09-19
您好,提交和删除没有关系。‘delete//’应该改成'delete/<pk>/' 都要以一个/斜杠结尾
还有,确认一下你本地代码更改之后,同步更新到服务器上去了。
122020-09-22 -
慕容2094767
提问者
2020-09-18
delete/<pk>/ ,delete路由两个//之间本来是有 <pk> 这个参数的,不知道为啥问题提交之后这个地方参数被删除了。
10
相似问题