django - Django-rest-framework 使用 3 种内容类型如何管理渲染器和序列化器
问题描述
我有一个带有django-rest-framework
(最新版本)的 api,我需要根据所content-type
问的不同响应。
现在,我期望application/vnd.api+json
或text/html
(用于可浏览的 api)。但我需要为application/vnd.oasis.stix+json; version=2.0
.
我试图添加一个新的自定义渲染器,但它只允许我指定“application/json”。我已经创建了这个,但它需要所有json
内容。
class StixRenderer(JSONRenderer):
media_type = 'application/vnd.oasis.stix+json'
def render(self, data, media_type=None, renderer_context=None):
return super(StixRenderer, self).render(data, media_type, renderer_context)
我知道我可以发送不同的序列化程序,但我不知道如何区分不同的内容类型。
解决方案
推荐阅读
- r - Shinymeta:如何在模块中使用 expandChain?
- reactjs - useCallback 和 useMemo 有什么区别
- c# - 在检查器中获取当前按下的键
- c# - 如何将 Saxon XdmNode 转换为 XmlDocument?
- python - Keras CNN 用于图像转换、NAN 损失
- c - ESP32-CAM 掉帧
- android - 如何使用 Firestore 数据库和 Firebase 身份验证创建登录方法
- javascript - TypeError:刷新或操作后无法读取属性
- javascript - 全屏隐藏播放器控件
- python - Python函数为列表中的每个区域创建一个图