| | 1 | = Django and AJAX = |
| | 2 | This middleware is another attemp to simplify using AJAX with Dajngo based on JSON-RPC specification (http://json-rpc.org/). It inspired by Java Struts2 JSON-plugin by Musachy Barroso (http://cwiki.apache.org/S2PLUGINS/json-plugin.html) and |
| | 3 | SimpleJSONRPCServer by David McNab (http://code.djangoproject.com/wiki/JSON-RPC). |
| | 4 | = Basic configuration = |
| | 5 | * jsonrpcserver.py must be on your PYTHONPATH. |
| | 6 | * Put class 'jsonrpcserver.JSONRPCServerMiddleware' to Django middleware list in settings.py: |
| | 7 | {{{ |
| | 8 | #!python |
| | 9 | MIDDLEWARE_CLASSES = ( |
| | 10 | 'django.middleware.common.CommonMiddleware', |
| | 11 | 'django.contrib.sessions.middleware.SessionMiddleware', |
| | 12 | 'django.contrib.auth.middleware.AuthenticationMiddleware', |
| | 13 | 'django.middleware.transaction.TransactionMiddleware', |
| | 14 | "jsonrpcserver.JSONRPCServerMiddleware", |
| | 15 | ) |
| | 16 | |
| | 17 | }}} |
| | 18 | * Add 'jsonrpc_urlpatterns' to 'urls.py' with 'urlpatterns' syntax: |
| | 19 | {{{ |
| | 20 | #!python |
| | 21 | from django.conf.urls.defaults import * |
| | 22 | |
| | 23 | urlpatterns = patterns('', |
| | 24 | #standard django url configuration here |
| | 25 | #... |
| | 26 | ) |
| | 27 | |
| | 28 | jsonrpc_urlpatterns = patterns('', |
| | 29 | (r'^myapp/json_rpc_service/$', 'myapp.newapp.json_rpc_views'), |
| | 30 | (r'^myapp/json_rpc_geoservice/$', 'myapp.geo.views'), |
| | 31 | ) |
| | 32 | }}} |
| | 33 | note: 'myapp.newapp.json_rpc_views' IS NOT A FUNCTION, BUT A MODULE, that contains exposed JSON-RPC functions. |
| | 34 | = Optional Django settings = |
| | 35 | * JSONRPC_URLPATTERNS_NAME: |
| | 36 | Default value: 'jsonrpc_urlpatterns'. |
| | 37 | Name of the variable in 'urls.py',that contains JSON-RPC URL patterns. |
| | 38 | URL pattern must have Django syntax, but they must contain |
| | 39 | MODULES NAMES, NOT FUNCTION NAMES as second element in tuple for patterns function. |
| | 40 | * JSONRPC_SERIALIZER: |
| | 41 | Default value: JSONSerializer class - thin wrapper over django.utils.simplejson. |
| | 42 | Name of the JSON serializer class, that must have methods 'serialize' and 'deserialize' |
| | 43 | * JSONRPC_JSONFY_AT_ALL_COSTS: |
| | 44 | Default value: False. |
| | 45 | Boolean flag, that determines whether JSON encoder should throwing an error or |
| | 46 | returning a str() representation of unsupportable python object. |
| | 47 | See implementation of AugmentedJSONEncoder class. |
| | 48 | = Usage = |
| | 49 | * Write exposed function im module, listed in jsonrpc_urlpatterns: |
| | 50 | {{{ |
| | 51 | #!python |
| | 52 | from jsonrpcserver import jsonrpc_function |
| | 53 | @jsonrpc_function |
| | 54 | def my_python_method(par1, par2): |
| | 55 | return par1 + par2 |
| | 56 | ... |
| | 57 | }}} |
| | 58 | * use from JS client (e.g. dojo): |
| | 59 | {{{ |
| | 60 | #!python |
| | 61 | var service = new dojo.rpc.JsonService({ |
| | 62 | "serviceType": "JSON-RPC", |
| | 63 | "serviceURL": "/myapp/json_rpc_service/" |
| | 64 | }); |
| | 65 | service.callRemote("my_python_method", ["string1", "string2"]) |
| | 66 | .addCallback(...); |
| | 67 | }}} |
| | 68 | = Example = |
| | 69 | See example here: http://alx3apps.appspot.com/jsonrpc_example/. |
| | 70 | = Other = |
| | 71 | Written by alx3 (alx3apps(at)gmail(dot)com). |
| | 72 | You can use this code under the terms of BSD licence (like Django project). |
| | 73 | |