Giter Club home page Giter Club logo

django-ajax's Introduction

django-ajax

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.

Caution!

If this middleware is activated you cannot use the @ajax decorator. That will cause double JSON conversion.

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!

django-ajax's People

Contributors

yceruto avatar kavdev avatar luisza avatar duduklein avatar furious-luke avatar yasminvc avatar akashkore5 avatar b3ni avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.