在django中处理mimetypes的正确方法是什么

时间:2014-03-06 14:20:02

标签: 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_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 ...

哪种看起来不正确,尤其是格式调用。

这种做法的正确方法是什么?

0 个答案:

没有答案