Skip to main content

Django Asynchronous Communication with jQuery/Ajax

Project description

# Cajax
[![Build Status](]( [![Gitter](](

This amazing library was developed to facilitate communication between your Django Server and your Website through AJAX requests. Check below the wiki to view the operation of the library.

## Manual
Here is a manual how it works.

### Installation

Download and install package:
$ pip install django-cajax

Through Github:
$ pip install -e git://

### Configure Cajax on your Django Project

urlpatterns += [
url(r'^', include('cajax.urls')),




{% load cajax %}
<p>My Page!</p>
<div id="val"></div>
<p>End of my page.</p>
<script type="text/javascript" src="/static/js/jquery.js"></script> <!-- needs jQuery library -->
{% cajax csrf_token %} <!-- import the library core, with csrf_token -->

### Using Cajax
Create a file called "" for each app that you want to use Cajax.
The Cajax Core will import all the methods in these files.
--app1 <-- django app NOT using cajax
--app2 <-- django app using cajax <-- put your specific methods here
--app3 <-- django app using cajax <-- put your specific methods here

In this file, you'll create a new type of views on Django, called "Cajax Views". These views are executed by Cajax Core.
Each view will receive two arguments:
* request: Django Request Instance, like a normal django view;
* cajax: Cajax Core Instance (See attributes and methods avaliable below)

Don't worry about return anything.

import json # use json.dumps() to send dictionarys

def my_cajax_view(request, cajax):
info = "important text"
result =['value1'] +['value2']
my_dict = {
'result': result

cajax.script("alert('Welcome "+['name'] +"!');")
cajax.script("on_client('Result: "+ result +"');")
cajax.script("with_dictionary("+ json.dumps(my_dict) +");")
cajax.html("#val", '<b>info</b>')

# More Methods
# cajax.render('#id', 'template.html', {'value': 12})
# cajax.html('.class', '<b>Hello World!</b>')
# cajax.add_css_class('a', 'css-class')
# cajax.redirect('')
# cajax.get_response()

Now on your template, call cajax(view) or cajax(view, data).
function on_client(string) {

function with_dictionary(dict) {

// Cajax Calls
// or
cajax("my_cajax_view", {'name': 'João', 'value1': 2, 'value2': 3});


## Cajax Core Attributes and Methods

The Cajax Core has the following attributes and methods avaliable to use:

Attribute | Description
--------- | -----------
data | Dicionary with data passed by cajax(url, data)

Method | Description
------ | -----------
get_response() | Return the HttpResponse that will be returned on the end. USE WITH CAUTION!
clean() | Erase all data on response
script(code) | Put a raw javascript code on response
redirect(url) | Redirect page to **url**
show(selector) | Force *display: block;* on node
hide(selector) | Force *display: none;* on node
prepend(selector, value) | Prepend value on node
append(selector, value) | Append value on node
assign(selector, attribute, value) | Put a value on attribute of each node
add_css_class(selector, class) | Add class on node
remove_css_class(selector, class) | Remove class on node
html(selector, html) | Put html value on node
render(selector, template, context={}) | Render a template using context with Django Template on node

## Contributors
* Felipe Lima Morais
* ElaboraInfo
* Elabora Consultoria Ltda
* Gabriel de Biasi

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

django-cajax-1.0.21.tar.gz (4.8 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