django - 如何在 Django 中返回自定义响应/状态代码?
问题描述
我想返回不同于100 <= self.status_code <= 599
Django 的 HttpResponseBase类中所写的状态代码。有什么简单的方法可以绕过这个限制吗?
解决方案
如果你真的想要,你可以修补.status_code
属性。例如:
from django.http import HttpResponse
def some_view(request):
response = HttpResponse('some data')
response.status_code = 754 # sample status code
return response
但是在 100-599 范围之外指定HTTP 状态代码 [wiki]没有多大意义,因为这些被细分为指定在什么情况下返回什么状态代码的子范围。
推荐阅读
- java - 如何从@ToString lombok 注解生成的字符串转换为对象?
- javascript - 为什么在动态创建按钮时出现“无法读取 null 的属性 'appendChild'”?
- bigcommerce - 需要在结帐页面中建立日历
- python - 为什么在 tkinter 中查找中位数时会出错?
- c++ - 通过带有 < 的 stdin/cin 捕获文件并读取到 EOF
- javascript - 获取键和值在 IE 中使用 JavaScript 拖放地图
- babeljs - 如何在 next.config.js 或 babelrc 中启用@experimentalDecorators
- java - hybris中部署代码的用途是什么?
- r - RMarkdown 软件包 flextable 立即失败,并显示“read_docx only support docx”
- html - 桌面查看不同视频和移动查看不同视频自动播放