确定请求的内容类型? [英] Determine the requested content type?

查看:95
本文介绍了确定请求的内容类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个Django视图,它根据所要求的内容提供变体内容。例如,对于text / xml,为text / json提供XML,提供JSON等。有没有办法从请求对象中确定?这样做会很棒:

I'd like to write a Django view which serves out variant content based on what's requested. For example, for "text/xml", serve XML, for "text/json", serve JSON, etc. Is there a way to determine this from a request object? Something like this would be awesome:

def process(request):
    if request.type == "text/xml":
        pass
    elif request.type == "text/json":
        pass
    else:
        pass

为此,在 HttpRequest 上有一个属性吗?

Is there a property on HttpRequest for this?

推荐答案

HttpRequest.META ,更具体地说是 HttpRequest.META.get( 'HTTP_ACCEPT')—而不是 HttpRequest.META.get('CONTENT_TYPE') 如前所述

HttpRequest.META, more specifically HttpRequest.META.get('HTTP_ACCEPT') — and not HttpRequest.META.get('CONTENT_TYPE') as mentioned earlier

这篇关于确定请求的内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆