python - 如何解决“没有返回 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"),
...
解决方案
你的函数不返回任何东西!!!
你必须这样做:
return redirect('ocr', comment.doc_id.id)
例如 :
def my_view(request):
...
return redirect('/some/url/')
def my_view(request):
...
return redirect('https://example.com/')
推荐阅读
- php - 教义 orm 映射返回错误未定义索引
- java - 有没有办法将文本作为 html 共享到 whatsapp
- java - 我可以得到另一个文件的目录吗?
- javascript - 获取存储在隐藏输入中的对象数组
- react-native-android - 元素类型无效:需要一个字符串,确实需要帮助
- java - 我自己的 AppcompatDialog 生成了一个非常丑陋的阴影/高度
- outlook - 如何在 Outlook 中每 24 小时运行一次规则
- php - 如何在codeigniter的特定文件夹中将mysql结果保存为csv
- c# - 使用模型自动属性时如何将数据从模型传递到视图模型?
- kendo-ui - 更改列标题 Treelist KendoUI