Adjax Framework Tutorial¶
This tutorial will help you add some basic Ajax functionality to your site, introducing you to the Adjax framework. It assumes you have already installed the Adjax framework. The tutorial also assumes you know Django well enough to create your own app using views, url config and templates.
Writing an Ajax view¶
An ajax view is just like any other view in Django, a simple function that takes a request and returns a response. Instead of rendering a template and returning an HttpRequest() object directly (or through
Create the following view in your app:
import adjax def hello_world(request): adjax.replace(request, 'h1', 'Hello world') return adjax.response(request)
from django.shortcuts import render_to_response from django.template.context import RequestContext def index(request): return render_to_response('index.html', context_instance=RequestContext(request))
And a simple
urls.py which will connect our two views:
from django.conf.urls.defaults import * urlpatterns = patterns('myapp.views', url(r'^$', 'index', name="index"), url(r'^hello_world/$', 'hello_world', name="hello_world"), )
index.html template, adding the following HTML:
This template contains four important elements:
- our h1 tag, which will be replaced
- a link to start the Ajax query when the user feels like it
Fire up the django development server and point your browser at
You will be shown a very simple page with our old heading and a link. If you click the link the text will be replaced, without refreshing the entire page.
More examples please¶
More examples can be found in the test suite, which contains a working demonstration and tests of all of Adjax features.
Play with the demonstation (
http://localhost:8000/demo), have a read through the views and templates to see how it can be used.
What else is possible?¶
- replacing HTML elements (what we just did)
- hiding HTML elements
- displaying messages to the user
- updating displayed information from a Django model instance
- submitting and validating forms
- redirecting the browser
- providing custom information in the JSON response
- including django templates