Skip to main content

A Django application to provide AJAX autocomplete with Select2.

Project description

Light integration glue between Django and Select2.

This project is inspired by Django-Select2 and django-select2light.

Django-select2-rocks is distributed under the BSD 2-clause license.

Installation

  1. pip install django_select2_rocks

  2. add select2rocks to your INSTALL_APPS

  3. python manage.py collectstatic will install Django Select2 Rocks JS.

  4. Include jQuery (1.7+), Select2 JS/CSS (not provided, tested with v3.4.5) and select2rocks/select2rocks.js in your templates.

  5. Now, you can use Select2ModelChoiceField fields in your forms.

Design

Django-select2-rocks provides widgets to render and initialize Select2 inputs.

The widget rendering is based on Django with a further step which adds a JS initialization for each input (on DOM ready). When a character is typed in the input field, an AJAX request is sent to the URL associated to the field.

The widget API is designed to allow you to pass any arguments you want to Select2 JS code (eg. allowClear option).

You can use various JSON views to answer to Select2 AJAX queries, and so to adjust search terms or the format of the results, it’s possible to extend the default django-select2-rocks backend (see select2rocks-backends.js in testproj for an example).

Example

The testproj project contains examples with a simple JSON view, a Tastypie and a Django REST framework view.

import select2rocks

class BeachForm(forms.Form):
    beach = select2rocks.Select2ModelChoiceField(
        queryset=Beach.objects.all(),
        widget=select2rocks.AjaxSelect2Widget(url_name='json_beaches'))

Subtleties

Beware, the internal representation of the fields uses commas (,) to separate items. Thus you cannot use commas in your labels. Otherwise we’ll interpret it as a separator and try to work with the text on both sides of the comma, which will most likely break.

Get the Code

Django-select2-rocks is developed on GitHub:

https://github.com/polyconseil/django-select2-rocks

You can either clone the public repository:

$ git clone git://github.com/polyconseil/django-select2-rocks.git

Once you have a copy of the source, you can install it with:

$ python setup.py install

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

django-select2-rocks-0.8.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

django_select2_rocks-0.8.0-py2-none-any.whl (6.9 kB view details)

Uploaded Python 2

File details

Details for the file django-select2-rocks-0.8.0.tar.gz.

File metadata

  • Download URL: django-select2-rocks-0.8.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.14

File hashes

Hashes for django-select2-rocks-0.8.0.tar.gz
Algorithm Hash digest
SHA256 c14b663fefdff7ce597fe17c164aac7a579ecfe434be3a838602be409d7a11dc
MD5 ac4ca8daba85af69b4a5cd17d048acfe
BLAKE2b-256 0672ba23578dc8b7e87a43cfb8e896eeda28dc1779e73a0bc357508e94f5aad6

See more details on using hashes here.

File details

Details for the file django_select2_rocks-0.8.0-py2-none-any.whl.

File metadata

  • Download URL: django_select2_rocks-0.8.0-py2-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.14

File hashes

Hashes for django_select2_rocks-0.8.0-py2-none-any.whl
Algorithm Hash digest
SHA256 d4082f637a731ee0a4ed234b9ca6189f065117c85c28c7256c0dd47be833beb7
MD5 6ad9fac44dbd6bbb13aa9d31f8c31f11
BLAKE2b-256 e8fbca2e3564e858b0470979407a6f0d37c6d0443622c35af02f1253f2748c13

See more details on using hashes here.

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