DjangoRpc
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.
Installation
Install using pip:
pip install DjangoRpc
…or clone the project from github:
https://github.com/Alerion/Django-RPC.git
Add djangorpc application to INSTALLED_APPS settings:
INSTALLED_APPS = ( ... 'djangorpc', )
Now all required JS files are accessible via Django staticfiles application.
Add jQuery to your page. You can use one from application for quick start:
<script src="{% static 'djangorpc/js/jquery-1.9.1.min.js' %}"></script>In your base templates add required JS scripts:
<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>
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); });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 which calls server-side method using Django RPC and shows us alert with received message.
Create rpc.py in your project folder with following code:
from djangorpc import RpcRouter, Msg
class MainApiClass(object):
def hello(self, username, user):
return Msg(u'Hello, %s!' % username)
rpc_router = RpcRouter({
'MainApi': MainApiClass(),
})
Add this to urls.py:
from django.conf.urls import patterns, include, url
from rpc import rpc_router
urlpatterns = patterns('someproject.someapp.views',
url(r'^rpc/', include(rpc_router.urls))
)
Add following code to page template:
<script src="{% url 'jsapi' %}"></script>
<script>
MainApi.hello('username', function(resp, sb){
alert(resp.msg);
});
</script>
Reload page and you will see an alert with the message “Hello, username!”.
If you get an error 403, you may have forgotten about CSRF.
Here is an example of CSRF cookie injection:
<script>
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
$.ajaxSetup({
beforeSend: function (xhr, settings) {
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
},
dataType: 'json',
error:function(jqXHR, textStatus, errorThrown){
alert(textStatus +'\n'+ errorThrown)
}
});
</script>
The working project example you can find in our repo.
Contributing
Development for this software happend on github, and the main fork is currently at https://github.com/Alerion/Django-RPC
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.
Metadata
Release files for DjangoRpc 0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| DjangoRpc-0.4.tar.gz | 69.4 kB | Without provenance | Details |
Release files / DjangoRpc-0.4.tar.gz
| Download URL | DjangoRpc-0.4.tar.gz |
|---|---|
| Size | 69.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a7a3de3c5ffc8d6f8a18406f70baa5e631c8ee17c3bd4026c334a937fb4f2c9b
|
|
BLAKE2b-256 checksum How to use checksums |
62901965e782a4391b8fc3a1c3b29b4d93c847e647b5f85b0c93c7bb67a5d382
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for DjangoRpc 0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.