本文介绍了确定请求的内容类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想编写一个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.get( 'HTTP_ACCEPT')
—而不是如前所述
HttpRequest.META
, more specifically HttpRequest.META.get('HTTP_ACCEPT')
— and not as mentioned earlier
这篇关于确定请求的内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!