简体   繁体   中英

Content-Type and Representations

What is the common convention for supporting multiple representation (eg html, json, xml) for resources (eg blog, user) in django?

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

  • /<resource>.<format> , eg /blogs/123.xml
  • /<format>/<resource> , eg /xml/blogs/123
  • /<resource>?format=<format> , eg /blogs/123?format=xml

Should I just rely on the Content-Type passed parameter? What about having multiple mobile representation (eg iphone, mobile, palm) and full browser representation?

What about views? What's the convention for choosing the right templates without having a lot of if statements or much duplicate code.

What I might do, if this were to work out, is:

  • Your views look for the Accept header (I think that's what you were talking about) and decide which content-type to send back based on the Accept header.
  • You have a middleware which looks for an extension in the Request-URI, removes it, and adds the associated content-type to the request Accept header.

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. But aside from browser-generated requests, the content-types should be coming in through the Accept header.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM