JSON-RPC Server middleware

Django and AJAX

This middleware is another attemp to simplify AJAX usage with Django 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 SimpleJSONRPCServer by David McNab (http://code.djangoproject.com/wiki/JSON-RPC).

Basic configuration

  • jsonrpcserver.py must be on your PYTHONPATH.
  • Put class 'jsonrpcserver.JSONRPCServerMiddleware' to Django middleware list in settings.py:
  • Add 'jsonrpc_urlpatterns' to 'urls.py' with 'urlpatterns' syntax:
    from django.conf.urls.defaults import *
    urlpatterns = patterns('',
    #standard django url configuration here
    jsonrpc_urlpatterns = patterns('',
        (r'^myapp/json_rpc_service/$', 'myapp.newapp.json_rpc_views'),
        (r'^myapp/json_rpc_geoservice/$', 'myapp.geo.views'),

note: 'myapp.newapp.json_rpc_views' IS NOT A FUNCTION, BUT A MODULE, that contains exposed JSON-RPC functions.

Optional Django settings


Default value: 'jsonrpc_urlpatterns'. Name of the variable in 'urls.py',that contains JSON-RPC URL patterns. URL pattern must have Django syntax, but they must contain MODULES NAMES, NOT FUNCTION NAMES as second element in tuple for patterns function.


Default value: JSONSerializer class - thin wrapper over django.utils.simplejson. Name of the JSON serializer class, that must have methods 'serialize' and 'deserialize'


Default value: False. Boolean flag, that determines whether JSON encoder should throwing an error or returning a str() representation of unsupportable python object. See implementation of AugmentedJSONEncoder class.


  • Write exposed function im module, listed in jsonrpc_urlpatterns:
    from jsonrpcserver import jsonrpc_function
    def my_python_method(par1, par2):
        return par1 + par2
  • use from JS client (e.g. dojo):
    var  service = new dojo.rpc.JsonService({
        "serviceType": "JSON-RPC", 
        "serviceURL": "/myapp/json_rpc_service/"
    service.callRemote("my_python_method", ["string1", "string2"])


See example here: http://alx3apps.appspot.com/jsonrpc_example/.


Sources available in google svn: http://code.google.com/p/django-json-rpc-server/


Written by alx3 (alx3apps(at)gmail(dot)com). You can use this code under the terms of BSD licence (like Django project).

Last modified 14 years ago Last modified on 12/08/08 18:44:35

Attachments (1)

Download all attachments as: .zip

Back to Top