NOTE: This application works only with Jinja2 templates using the django_jinja package (https://github.com/niwibe/django-jinja).
You can install Django Auto Ajax with PIP:
pip install git+git://github.com/visipedia/django-auto-ajax
Add django_auto_ajax
to your INSTALLED_APPS
.
Add the following to your settings:
JINJA2_LOADER = 'django_auto_ajax.loaders.Loader'
JINJA2_EXTENSIONS = {
'django_auto_ajax.extensions.SnippetsExtension',
}
Include django_auto_ajax/js/ajax.js
to your page.
To ajaxify all your links with class ajax
add the following to your JavaScript file:
$('a[href].ajax').live('click', function (event) {
event.preventDefault();
$.get(this.href);
});
To ajaxify all your forms with class ajax
add the followint to your JavaScript file:
$("form.ajax").live('submit', function () {
$(this).ajaxSubmit();
return false;
});
To mark the pieces of template that you want to reload with AJAX use:
{% snippet 'snippet-name' %}
Any piece of template here...
{% endsnippet %}
Snippets can be anywhere, can be nested, etc., but each must have globally unique name.
In your views add from django_auto_ajax.response import *
and use it instead of Django's render/response/redirect methods.
To say which pices of template should be repainted on AJAX request use:
invalidate(request, 'snippet-name')
To return response from your views use the following methods.
A normal response:
return response(request, 'myapp/mytemplate.jinja', data=data)
In case of non-AJAX request the template will be rendered normally. In case of AJAX request only the invalidated snippets will be rendered.
Pure AJAX response:
return ajax_response(data=data)
Redirect:
return redirect(...) # same parameters as Django's redirect