Skip to main content

Django RPC for jQuery

Project description

Django RPC for jQuery. Allows execute some server-side class methods with Javascript in your browser.

Also you can submit forms with jQuery Form Plugin.

It also supports call batching. Django RPC batches together calls which are received within a configurable time frame and sends them in a single request.

You can easy move you current views to RPC methods and use Django RPC instead of mess of AJAX requests.

Inspired by Ext.Direct from ExtJs 3.

Package Documentation

Installation

Install using pip:

pip install DjangoRpc

…or clone the project from github:

https://github.com/Alerion/Django-RPC.git
  1. Add djangorpc application to INSTALLED_APPS settings:

    INSTALLED_APPS = (
        ...
        'djangorpc',
    )

Now all required JS files are accessible via Django staticfiles application.

  1. In your base templates add required JS scripts:

    <script src="{% static 'djangorpc/js/jquery-1.7.2.min.js' %}"></script>
    <script src="{% static 'djangorpc/js/jquery.util.js' %}"></script>
    <script src="{% static 'djangorpc/js/jquery.rpc.js' %}"></script>
    <script src="{% static 'djangorpc/js/jquery.form.js' %}"></script>
  2. You can handle all errors in one place and show some message to user:

    //Show error message for RPC exceptions
    jQuery.Rpc.on('exception', function(event){
        alert('Error during RPC request: '+event.message);
    });
  3. Do not forget about CSRF for Ajax requests.

Example

Let’s take a look at a quick example how to use Django RPC application.

We’ll create a page with button, which calls server-side method using Django RPC.

Create actions.py in someapp application of our someproject project with following code:

from djangorpc import RpcRouter, Error, Msg


class MainApiClass(object):

    def hello(self, username, user):
        return Msg(u'Hello, %s!' % username)

router = RpcRouter('main:router', {
    'MainApi': MainApiClass(),
})

Add this to urls.py:

from django.conf.urls import patterns, include, url
from .actions import router


urlpatterns = patterns('someproject.someapp.views',
    url(r'^router/$', router, name='router'),
    url(r'^router/api/$', router.api, name='api'),
)

Add following code to page template:

<script src="{% url 'main:api' %}"></script>
<script>
    MainApi.hello('username', function(resp, sb){
        alert(resp.msg);
    });
</script>

Reload page and you will see alert with message “Hello, username!”.

Contributing

Development for this software happend on github, and the main fork is currently at https://github.com/chrisglass/xhtml2pdf

Contributions are welcome in any format, but using github’s pull request system is very highly preferred since it makes review and integration much easier.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

DjangoRpc-0.1.1.tar.gz (180.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page