[英]Content-Type and Representations
What is the common convention for supporting multiple representation (eg html, json, xml) for resources (eg blog, user) in django? 在django中支持资源(例如博客,用户)的多重表示(例如html,json,xml)的常见惯例是什么?
First, I don't know how I should format my urls. 首先,我不知道应该如何格式化我的网址。 For example, what your take on using either of these urls to request xml format
例如,您使用这些URL中的任何一个来请求xml格式
/<resource>.<format>
, eg /blogs/123.xml
/<resource>.<format>
,例如/blogs/123.xml
/<format>/<resource>
, eg /xml/blogs/123
/<format>/<resource>
,例如/xml/blogs/123
/<resource>?format=<format>
, eg /blogs/123?format=xml
/<resource>?format=<format>
,例如/blogs/123?format=xml
Should I just rely on the Content-Type
passed parameter? 我应该只依赖
Content-Type
传递的参数吗? What about having multiple mobile representation (eg iphone, mobile, palm) and full browser representation? 那么有多个移动表示(例如iphone,mobile,palm)和完整的浏览器表示呢?
What about views? 观点怎么样? What's the convention for choosing the right templates without having a lot of if statements or much duplicate code.
在没有大量if语句或重复代码的情况下选择正确模板的惯例是什么。
What I might do, if this were to work out, is: 如果要解决这个问题,我可能会做的是:
Accept
header (I think that's what you were talking about) and decide which content-type to send back based on the Accept
header. Accept
标头(我认为这就是您所说的内容)并根据Accept
标头决定要发回的内容类型。 Accept
header. Accept
标头中。 For this solution, content-types in the URL would always be represented as an associated file extension, neither part of the query-string nor part of the resource name. 对于此解决方案,URL中的内容类型将始终表示为关联的文件扩展名,既不是查询字符串的一部分,也不是资源名称的一部分。 But aside from browser-generated requests, the content-types should be coming in through the
Accept
header. 但是除了浏览器生成的请求之外,内容类型应该通过
Accept
标头进入。
So the request comes in as: 所以请求来自:
GET /blogs/123.xml HTTP/1.1
Host: example.com
The middleware transforms that to: 中间件将其转换为:
GET /blogs/123 HTTP/1.1
Host: example.com
Accept: application/xml
Your view sees application/xml
and returns a response with XML content. 您的视图将看到
application/xml
并返回包含XML内容的响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.