Skip to main content

Powerful and easy AJAX framework for django applications.

Project description

Fast and easy AJAX libraries for django applications.

Build Status PYPI Package PYPI Status PYPI License

Requirements

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 its advantages!

Usage

@ajax Decorator

from django_ajax.decorators import ajax

@ajax
def my_view(request):
    do_something()

When nothing is returned as result of view then returns (JSON format):

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

Sending custom data in the response

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

The result is send to the browser in the following way (JSON format)

{"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 JSON response:

{"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 use AJAX quite frequently in your project, we suggest using the AJAXMiddleware described below.

Add django_ajax.middleware.AJAXMiddleware into the MIDDLEWARE_CLASSES list in settings.py.

All your responses will be converted to JSON if the request was made by AJAX, otherwise is return a HttpResponse.

AJAXMixin for class-based views

AJAXMixin is an object that calls the 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!

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

djangoajax-2.4.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

djangoajax-2.4-py3.6.egg (35.9 kB view details)

Uploaded Source

File details

Details for the file djangoajax-2.4.tar.gz.

File metadata

  • Download URL: djangoajax-2.4.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for djangoajax-2.4.tar.gz
Algorithm Hash digest
SHA256 9a5fe84354492007bd30d8327a606b1b94ceb0fafb9d76b371ecea4d7d9f28c3
MD5 ff91c05e78db7746cfbef47179124603
BLAKE2b-256 36dcb59ca11beb4a6827d4ef196260702e0d377d0ae1113a81479903ad9d8e66

See more details on using hashes here.

Provenance

File details

Details for the file djangoajax-2.4-py3.6.egg.

File metadata

  • Download URL: djangoajax-2.4-py3.6.egg
  • Upload date:
  • Size: 35.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for djangoajax-2.4-py3.6.egg
Algorithm Hash digest
SHA256 d97b03a429c07cf52130ab570a6af9d111658c24db15d8b07dbb78031bd703b2
MD5 cb339633d634b843e23fe06e9100da29
BLAKE2b-256 341ddfbbe3c6c2f212e9e3521303ed978472c6f28f30742dbcc03bb351bf50bc

See more details on using hashes here.

Provenance

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