Version 9 (modified by alx3, 7 years ago) (diff)



Django and AJAX

This middleware is another attemp to simplify AJAX usage with Django based on JSON-RPC specification ( It inspired by Java Struts2 JSON-plugin by Musachy Barroso ( and SimpleJSONRPCServer by David McNab (

Basic configuration

  • must be on your PYTHONPATH.
  • Put class 'jsonrpcserver.JSONRPCServerMiddleware' to Django middleware list in
  • Add 'jsonrpc_urlpatterns' to '' 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 '',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:


Use sources from google svn: There is an old version in attachment.


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

Attachments (1)

Download all attachments as: .zip

Back to Top