Login

Tag "accept"

Snippet List

Improved Accept middleware with webkit workaround

An accept middleware, which is based on the code of http://djangosnippets.org/snippets/1042/ but adds a workaround for the buggy accept header, sent from webkit browsers such as safari and chrome. The workaround affects any accept header, that has xml and (x)html in the best q, but also the xml mediatype at first place in the list. If this is the case, the header is rearanged, by shifting the xml mediatype to become the last element of the best quality entries in the header. If the workaround did manipulate the header, and there is a html entry in the list with lower quality as an xhtml entry that is also in the list (with best q), then the html entry is also raised in q to be one entry in front of xml.

  • middleware
  • accept
  • header
  • webkit
Read More

Improved Accept header middleware

A clean and simple implementation of parsing the Accept header. It places the result in request.accepted_types. Place this middleware anywhere in the chain, as all it does is add to the request object.

  • middleware
  • accept
  • header
Read More

Accept Header Middleware

A middleware that parses the HTTP_ACCEPT header of a request. The request gets a new method called "accepts" that takes a string and returns True if it was in the list of accepted mime-types. It makes it possible to write views like: def exampleview(request): if request.accepts('application/json'): # return a json representation if request.accepts('text/html'): # return html Please note that with this middleware the view defines the priority of the mime-types, not the order in which they where provided in the HTTP-Header.

  • middleware
  • request
  • accept
Read More

3 snippets posted so far.