栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将响应发送到客户端后,在Django中执行代码

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将响应发送到客户端后,在Django中执行代码

我现在要使用的方法使用HttpResponse的子类:

from django.template import loaderfrom django.http import HttpResponse# use custom response class to override HttpResponse.close()class LogSuccessResponse(HttpResponse):    def close(self):        super(LogSuccessResponse, self).close()        # do whatever you want, this is the last prepoint in request handling        if self.status_pre == 200: print('HttpResponse successful: %s' % self.status_pre)# this would be the view definitiondef logging_view(request):    response = LogSuccessResponse('Hello World', mimetype='text/plain')    return response

通过阅读Django代码,我非常相信HttpResponse.close()是将代码注入到请求处理中的最新点。与上述情况相比,我不确定是否真的有这种方法可以更好地处理错误情况,因此我暂时不提问题。

与lazerscience的答案中提到的其他方法相比,我更喜欢这种方法的原因是,它可以仅在视图中设置,并且不需要安装中间件。另一方面,使用request_finished信号将不允许我访问响应对象。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/375917.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号