Skip to main content

Fast and easy AJAX libraries for django applications.

Master Build Status PYPI Package PYPI Status PYPI License

Requirements

3.x

2.x

Installation

Install django-ajax in your python environment

1- Download and install package:

$ pip install djangoajax

Through Github:

pip install -e git://github.com/yceruto/django-ajax#egg=djangoajax

or simply with:

$ python setup.py install

2- Add 'django_ajax' into the INSTALLED_APPS list.

3- Read usage section and enjoy this feature!

Usage

@ajax Decorator

from django_ajax.decorators import ajax

@ajax
def my_view(request):
    do_something()

When the view does not return anything, you will receive this response (JSON format):

{"status": 200, "statusText": "OK", "content ": null}

Sending content

@ajax
def my_view(request):
    c = 2 + 3
    return {'result': c}

The whole result is converted into a JSON format as part of the content element:

{"status": 200, "statusText": "OK", "content": {"result": 5}}

Combining with others decorators

from django.contrib.auth.decorators import login_required
from django_ajax.decorators import ajax

@ajax
@login_required
def my_view(request):
    # if the request.user is anonymous then this view not proceed
    return {'user_id': request.user.id}

The location or path of the redirection response will be given in the content item, also the status and statusText will reflect what is going on:

{"status": 302, "statusText": "FOUND", "content": "/login"}

Template response

from django.shortcuts import render
from django_ajax.decorators import ajax

@ajax
def my_view(request):
    return render(request, 'home.html')

The JSON response:

{"status": 200, "statusText": "OK", "content": "<html>...</html>"}

Catch exceptions

@ajax
def my_view(request):
    a = 23 / 0  # this line throws an exception
    return a

The JSON response:

{"status": 500, "statusText": "INTERNAL SERVER ERROR", "content": "integer division or modulo by zero"}

AJAXMiddleware

If you are using AJAX at all times in your project, we suggest you activate the AJAXMiddleware described below.

Add django_ajax.middleware.AJAXMiddleware to the MIDDLEWARE_CLASSES list in settings.py and all your responses will be converted to JSON whereas the request was made via AJAX, otherwise it will return a normal HttpResponse.

AJAXMixin for class-based views

AJAXMixin is an object that call to AJAX decorator.

from django.views.generic import TemplateView
from django_ajax.mixin import AJAXMixin

class SimpleView(AJAXMixin, TemplateView):
    template_name = 'home.html'

The JSON response:

{"status": 200, "statusText": "OK", "content": "<html>...</html>"}

Enjoy And Share!

Release files for djangoajax 3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for djangoajax 3.3
File Size Uploaded
djangoajax-3.3.tar.gz 14.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for djangoajax 3.3
File Interpreter ABI Platform
djangoajax-3.3-py3-none-any.whl Python 3 none any Details

Total release size: 36.2 kB

Release files / djangoajax-3.3.tar.gz

Download URL djangoajax-3.3.tar.gz
Size 14.9 kB
Tags Source
SHA-256 checksum
How to use checksums
85d76a01a81fb999ecd3fe7c216946bd331600d80c2694ad63972b49f4043ea4
BLAKE2b-256 checksum
How to use checksums
71c51356ec349740b398ef04834e3b08edd65e62544cbe07c2d3094a7b4ece8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.10

Release files / djangoajax-3.3-py3-none-any.whl

Download URL djangoajax-3.3-py3-none-any.whl
Size 21.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6e1041d70602fc13494e696c20940007c870d5e5cbfd2cc14d91816effac1c41
BLAKE2b-256 checksum
How to use checksums
6ed027a6110be4c57e2f19d4ab7ec8ebf325c28be8d5a00ce7c83170b23c248f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.10

Release history Release notifications | RSS feed

This release

3.3 This release

2 release files

3.2

1 release file

3.1

1 release file

3.0.3

1 release file

3.0.2

1 release file

3.0.1

1 release file

3.0

2 release files

2.4.6

1 release file

2.4.5

1 release file

2.4.4

1 release file

2.4.3

1 release file

2.4.2

1 release file

2.4.1

1 release file

2.4

2 release files

2.3.7

1 release file

2.3.6

1 release file

2.3.5

1 release file

2.3.4

2.3.3

1 release file

2.3.2

1 release file

2.3.1

2.3.0

1 release file

2.2.15

1 release file

2.2.14

2.2.13

1 release file

2.2.12

1 release file

2.2.11

1 release file

2.2.10

1 release file

2.2.9

1 release file

2.2.8

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page