首页 > 解决方案 > 如何解决“没有返回 HttpResponse 对象”错误?

问题描述

我有一个删除按钮。我在其他视图中使用相同的结构,并且它工作正常。但是在这个页面中它不起作用。当我想删除评论时出现错误:

/comment/18/analysis/ 处的 ValueError 视图 ocr.views.delete_approval_comment 未返回 HttpResponse 对象。它返回 None 。

它删除了评论,但给出了这个错误。我该如何解决?

视图.py

def delete_approval_comment(request,id):
    comment = CommentFromOthers.objects.get(id=id)
    comment.delete()
    redirect('ocr', comment.doc_id.id)

ocr.html

...
<a href="{% url 'delete_app_comment' comment.id %}" class="btn btn-sm btn-danger"
                                               onclick="return confirm('Are you sure you want to delete this?')"
>
    <i class="fa fa-trash" aria-hidden="true"></i>
</a>
...

网址.py

...
url(r'^ocrs/(?P<id>\d+)/analysis/$', views.ocr, name="ocr"),
url(r'^comment/(?P<id>\d+)/analysis/$', views.delete_approval_comment, name="delete_app_comment"),
...

标签: pythondjango

解决方案


你的函数不返回任何东西!!!

你必须这样做:

return redirect('ocr', comment.doc_id.id)

例如 :

def my_view(request):
    ...
    return redirect('/some/url/')

def my_view(request):
    ...
    return redirect('https://example.com/')

更多解释可以阅读Django文档


推荐阅读