Ajax goodies for django projects.
Project description
django-ajax-toolkit
Dependencies
msgpack-python>=0.2.4
django>=1.3
CI status (Travis)
Installation
Grab it from pypi with:
pip install django-ajax-toolkit
or:
easy_install django-ajax-toolkit
Returning data objects in views
JsonResponse
If you want to extend your views to work with ajax you may choose to return json data in your response. To make this easier you can use JsonResponse found in ajaxtoolkit.http:
from ajaxtoolkit.http import JsonResponse class MyView(TemplateView): def get(self, request, *args, **kwargs): if request.is_ajax: context = self.get_context_data() return JsonResponse(context) # ...
This will set the correct mimetype (application/json) and serialise your context data into a json object.
MsgpackResponse
MsgpackResponse works in a similar way to JsonResponse, but uses msgpack to provide with binary serialisation. The usage is the same as with JsonResponse:
def get(self, request, *args, **kwargs): if request.is_ajax: context = self.get_context_data() return MsgpackResponse(context) # ...
Ajax Middleware
If you’re using Django’s messages framework, you can also add ajaxtoolkit.middleware.AjaxMiddleware in your middleware.
This will inject all messages generated in your request into your JsonResponse object:
from django.contrib import messages from ajaxtoolkit.http import JsonResponse class MyView(TemplateView): def get(self, request, *args, **kwargs): if request.is_ajax: context = self.get_context_data() messages.info(request, "This is very useful") messages.warning(request, "Be careful!") return JsonResponse(context) # ...
This would be rendered as the following:
{ //... 'django_messages': [ {"extra_tags": "info", "message": "This is very useful", "level": 20}, {"extra_tags": "warning", "message": "Be careful!", "level": 30} ] }
Bypassing the message middleware
If you want to send an http response without attaching messages you can do that by setting the message_support attribute of the response object:
context = self.get_context_data() response = JsonResponse(context) response.message_support = False return response
You can also choose to subclass the original response classes, eg.:
class MsgpackResponseWithoutMessages(MsgpackResponse): message_support = False # ...
Contribute
Clone, create a virtualenv and run:
make install
This will install all dependencies. You can then run the tests with:
./runtests.py
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
File details
Details for the file django-ajax-toolkit-0.3.0.tar.gz
.
File metadata
- Download URL: django-ajax-toolkit-0.3.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa476f76892486e4214ed9506c1d18b1fcad0014ffdbd4673efd8ba14de1b532 |
|
MD5 | 217c5a821738c7c710ea4d8dda8a16fe |
|
BLAKE2b-256 | 4aaad6242597f794a1e134f99876661b1cd7e3f307aca2a0dd002d6422e1942a |