Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

Utility app to support AJAX development in Django

Project description


django_ajax provides some basic structure to make working with AJAX (and Javascript in general) more easy.


django_ajax provides various utils.


render_to_json is both implemented as a view and a decorator. The view does the real work, while the decorator is a utility function which itself uses the view to convert objects to json.

The view expects two arguments:

  • request
  • the object to output as JSON

It sets some HTTP headers to make sure the JSON can be loaded everywhere and nothing is cached in the browser.

The decorator just calls the original view function. If the response is not already a HttpResponse, it will be passed through the view as described above.

{% json %}

Template tag to render various objects as JSON.


<script type="text/javascript">
        var obj_as_json = {% json object %};

<script type="text/javascript">
        {# Output "null" of variable does not exist (prevents JS error) #}
        var obj_as_json = {% json object or null %};

{% ajax_cache %}

Utility template tag to pass objects to some JS function. May be used to implement JS-side caching of objects. This will speed up JS-driven apps, as the template can put out JS-objects then they are needed anyways. The tag makes sure every object will only be cached once, multiple calls will only output one JS-call.


<script type="text/javascript">
        {% comment %}
                Will output the same as "{% json object %});"
                But only if object was not used in combination with
                "" before.
        {% endcomment %}
        {% ajax_cache object using "" %}


Like context_processors, but used to put variables into the JS context. Provides JS_CONTEXT via a context processor, which can be rendered using {% json JS_CONTEXT %}. Example:

<script type="text/javascript">
        var config = {% json JS_CONTEXT %};

Uses settings.AJAX_CONFIG_PROCESSORS, which uses the same syntax as for context processors.

dango_ajax comes with three ajax processors (living in django_ajax.ajax_processors):

  • media: provides MEDIA_URL
  • static: provides STATIC_URL
  • session: provides basic authentcation information
    • includes AJAX login/logout urls and views


Uses object.ajax_data() in various places to allow defining method to convert models/… into dictionaries, which can be converted to JSON. This way you may pass model instances directly to {% json %} and the model may define which data to output.

Project details

Download files

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

Files for django_ajax, version 0.3.0
Filename, size File type Python version Upload date Hashes
Filename, size django_ajax-0.3.0.tar.gz (5.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page