Python django中处理mimetype的正确方法是什么

Python django中处理mimetype的正确方法是什么,python,django,mime-types,Python,Django,Mime Types,目前,我们正在对入站数据执行类似的操作: content_type = request.META.get('CONTENT_TYPE', '') mime_type, mime_subtype, mime_params = mimeparse.parse_mime_type(content_type) if mime_type == "application" and mime_subtype== "json": encoding = mime_param

目前,我们正在对入站数据执行类似的操作:

    content_type = request.META.get('CONTENT_TYPE', '')
    mime_type, mime_subtype, mime_params = mimeparse.parse_mime_type(content_type)
    if mime_type == "application" and mime_subtype== "json":
        encoding = mime_params.get("charset", "utf-8")
        return json.loads(request.raw_post_data.decode(encoding))
    elif ...
这看起来可能是对的,但令人惊讶的是,我们使用的是第三方库,而不是Django或Python标准

在出站端,它如下所示:

    accept = request.META.get('HTTP_ACCEPT', "*/*")
    content_type = mimeparse.best_match(["application/json", ...], accept)
    if content_type == "application/json":
        content_type = "{0}; charset=utf-8".format(content_type)
        raw = json.dumps(data)
        return content_type, raw
    elif ...
这看起来不对,尤其是格式调用


做这件事的正确方法是什么?

是您想要的。Django Rest框架与此无关。我的误解。我期待着您的回答。至少Django Rest框架必须以某种方式处理这一点是正确的。以下是方法: