Note: This is a testing middleware. This snippets may be changed frequently later.
What's it
-----------
Sometimes I thought thow to easy the output data into another format, except html format. One way, you can use decorator, just like:
@render_template(template='xxx')
def viewfunc(request,...):
And the output data of viewfunc should be pure data. And if want to output json format, you should change the decorator to:
@json_response
def viewfunc(request,...):
I think it's not difficult. But if we can make it easier? Of cause, using middleware.
So you can see the code of `process_response`, it'll judge the response object first, if it's an instance of HttpResponse, then directly return it. If it's not, then get the format of requst, if it's `json` format, then use json_response() to render the result.
How to setup `request.format`? In `process_request` you and see, if the `request.REQUEST` has a `format` (you can setup it in settings.py with FORMAT_STRING option), then the `request.format` will be set as it. If there is not a such key, then the default will be `json`. So in your view code, you can just return a python variable, this middleware will automatically render this python variable into json format data and return.
For 0.2 it support xml-rpc. But it's very different from common implementation. For server url, you just need put the same url as the normal url, for example:
http://localhost:8000/booklist/ajax_list/?format=xmlrpc
Notice that the format is 'xmlrpc'. A text client program is:
from xmlrpclib import ServerProxy
server = ServerProxy("http://localhost:8000/booklist/ajax_list/?format=xmlrpc", verbose=True)
print server.booklist({'name':'limodou'})
And the method 'booklist' of server is useless, because the url has include the really view function, so you can use any name after `server`. And for parameters of the method, you should use a dict, and this dict will automatically convert into request.POST item. For above example, `{'name':'limodou'}`, you can visit it via `request.POST['name']` .
For `html` format, you can register a `format_processor` callable object in `request` object. And middleware will use this callable object if the format is `html`.
Intall
---------
Because the view function may return non-HttpResponse object, so this middleware should be installed at the end of MIDDLEWARE_CLASSES sections, so that the `process_response` of this middleware can be invoked at the first time before others middlewares.
And I also think this mechanism can be extended later, for example support xml-rpc, template render later, etc, but I have not implemented them, just a thought.
Options
---------
FORMAT_STRING used for specify the key name of format variable pair in QUERY_STRING or POST data, if you don't set it in settings.py, default is 'format'.
DEFAYLT_FORMAT used for default format, if you don't set it in settings.py, default is 'json'.
Reference
-----------
Snippets 8 [ajax protocol for data](http://www.djangosnippets.org/snippets/8/) for json_response