Задать вопрос

Тел: +7 965 3737 888





Format transition middleware

<p>Note: This is a testing middleware. This snippets may be changed frequently later.</p>
<h2>What's it</h2>
<p>Sometimes I thought thow to easy the output data into another format, except html format. One way, you can use decorator, just like:</p>
def viewfunc(request,...):

<p>And the output data of viewfunc should be pure data. And if  want to output json format, you should change the decorator to:</p>
def viewfunc(request,...):

<p>I think it's not difficult. But if we can make it easier? Of cause, using middleware. </p>
<p>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.</p>
<p>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.</p>
<p>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:</p>

<p>Notice that the format is 'xmlrpc'. A text client program is:</p>
from xmlrpclib import ServerProxy
server = ServerProxy("http://localhost:8000/booklist/ajax_list/?format=xmlrpc", verbose=True)
print server.booklist({'name':'limodou'})

<p>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'] .</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>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'.</p>
<p>Snippets 8 <a href="http://foothold.ru/knowledge/detail/ajax-protocol-for-data.html">ajax protocol for data</a> for json_response</p>

Вопрос полезен? Да0/Нет0