An open API service indexing awesome lists of open source software.

https://github.com/toastdriven/multiresponse

A Python class for Django that allows a request to provide content-type aware responses.
https://github.com/toastdriven/multiresponse

Last synced: 11 months ago
JSON representation

A Python class for Django that allows a request to provide content-type aware responses.

Awesome Lists containing this project

README

          

MultiResponse
=============

A Python class for Django to provide mime type aware responses. This allows a client to receive different responses based on the HTTP "Accept" header they send. This is used in place of ``render_to_response`` or a manual ``HttpResponse``.

Requirements
------------

* Python 2.5+ (lower versions may work but are untested.)
* Django 1.0+ (again, lower versions may work but are untested.)
* mimeparse 0.1.2+ - http://code.google.com/p/mimeparse/

Sample Usage:
-------------

from django.conf import settings
from django.shortcuts import render_to_response
from multiresponse import MultiResponse

def index(request, extension):
sample_people = [
{'name': 'Daniel', 'age': 26},
{'name': 'John', 'age': 26},
{'name': 'Jane', 'age': 20},
{'name': 'Bob', 'age': 35},
]

mr = MultiResponse(request)
mr.register('html', 'index.html')
mr.register('xml', 'people.xml')
mr.register('json', 'people.json')
mr.register('txt', 'people.txt')
return mr.render({
'people': sample_people,
})

Output
------

A HTTP GET to http://localhost:8000/ with a web browser would yield something like:

HTTP/1.0 200 OK
Date: Tue, 02 Dec 2008 05:39:53 GMT
Server: WSGIServer/0.1 Python/2.5.1
Content-Type: text/html; charset=utf-8



People



People





  • Daniel


  • John


  • Jane


  • Bob




However, a HTTP GET to http://localhost:8000/ via "curl -i -H 'Accept: application/xml' http://localhost:8000/" would yield:

HTTP/1.0 200 OK
Date: Tue, 02 Dec 2008 05:42:14 GMT
Server: WSGIServer/0.1 Python/2.5.1
Content-Type: application/xml; charset=utf-8



Daniel
26


John
26


Jane
20


Bob
35

And a HTTP GET to http://localhost:8000/ via Javascript might look like:

HTTP/1.0 200 OK
Date: Tue, 02 Dec 2008 05:42:47 GMT
Server: WSGIServer/0.1 Python/2.5.1
Content-Type: application/json; charset=utf-8

{
'people': [

{'name': 'Daniel', 'age': '26'},

{'name': 'John', 'age': '26'},

{'name': 'Jane', 'age': '20'},

{'name': 'Bob', 'age': '35'},

]
}