2012-09-14 06:34:36 +00:00
|
|
|
===========
|
|
|
|
|
Get Started
|
|
|
|
|
===========
|
|
|
|
|
|
|
|
|
|
Overview
|
|
|
|
|
--------
|
|
|
|
|
|
|
|
|
|
.. automodule:: django_select2
|
|
|
|
|
:members:
|
|
|
|
|
|
2019-12-13 13:52:03 +00:00
|
|
|
Assumptions
|
|
|
|
|
-----------
|
|
|
|
|
|
|
|
|
|
* You have a running Django up and running.
|
|
|
|
|
* You have form fully working without Django-Select2.
|
|
|
|
|
|
2012-09-14 06:34:36 +00:00
|
|
|
Installation
|
|
|
|
|
------------
|
|
|
|
|
|
2015-09-25 20:11:50 +00:00
|
|
|
1. Install ``django_select2``::
|
2012-09-14 06:34:36 +00:00
|
|
|
|
|
|
|
|
pip install django_select2
|
|
|
|
|
|
2015-09-25 20:11:50 +00:00
|
|
|
2. Add ``django_select2`` to your ``INSTALLED_APPS`` in your project settings.
|
2012-09-14 06:34:36 +00:00
|
|
|
|
2019-12-13 13:52:03 +00:00
|
|
|
3. Add ``django_select`` to your ``urlconf``::
|
2012-09-14 06:34:36 +00:00
|
|
|
|
2019-12-13 13:52:03 +00:00
|
|
|
path('select2/', include('django_select2.urls')),
|
2013-11-20 00:44:28 +00:00
|
|
|
|
2019-12-13 13:52:03 +00:00
|
|
|
You can safely skip this one if you do not use any
|
|
|
|
|
:class:`ModelWidgets <.django_select2.forms.ModelSelect2Mixin>`
|
2012-09-14 06:34:36 +00:00
|
|
|
|
2018-03-19 11:09:54 +00:00
|
|
|
Quick Start
|
|
|
|
|
-----------
|
|
|
|
|
|
|
|
|
|
Here is a quick example to get you started:
|
|
|
|
|
|
|
|
|
|
0. Follow the installation instructions above.
|
|
|
|
|
|
2019-12-13 13:52:03 +00:00
|
|
|
1. Replace native Django forms widgets with one of the several ``django_select2.form`` widgets.
|
|
|
|
|
Start by importing them into your ``forms.py``, right next to Django own ones::
|
2018-03-19 11:09:54 +00:00
|
|
|
|
2019-12-13 13:52:03 +00:00
|
|
|
from django import forms
|
|
|
|
|
from django_select2 import forms as s2forms
|
2018-03-19 11:09:54 +00:00
|
|
|
|
2019-12-13 13:52:03 +00:00
|
|
|
Then let's assume you have a model with a choice, a :class:`.ForeignKey`, and a
|
|
|
|
|
:class:`.ManyToManyField`, you would add this information to your Form Meta
|
|
|
|
|
class::
|
2018-03-19 11:09:54 +00:00
|
|
|
|
2019-12-13 13:52:03 +00:00
|
|
|
widgets = {
|
|
|
|
|
'category': s2forms.Select2Widget,
|
|
|
|
|
'author': s2forms.ModelSelect2Widget(model=auth.get_user_model(),
|
|
|
|
|
search_fields=['first_name__istartswith', 'last_name__icontains']),
|
|
|
|
|
'attending': s2forms.ModelSelect2MultipleWidget …
|
|
|
|
|
}
|
2018-03-19 11:09:54 +00:00
|
|
|
|
|
|
|
|
2. Add the CSS to the ``head`` of your Django template::
|
|
|
|
|
|
|
|
|
|
{{ form.media.css }}
|
|
|
|
|
|
|
|
|
|
3. Add the JavaScript to the end of the ``body`` of your Django template::
|
|
|
|
|
|
|
|
|
|
{{ form.media.js }}
|
|
|
|
|
|
|
|
|
|
4. Done - enjoy the wonders of Select2!
|
2013-11-08 02:34:03 +00:00
|
|
|
|
2012-09-14 06:34:36 +00:00
|
|
|
External Dependencies
|
|
|
|
|
---------------------
|
|
|
|
|
|
2019-10-08 19:41:37 +00:00
|
|
|
* jQuery (version >=2)
|
|
|
|
|
jQuery is not included in the package since it is expected
|
2019-10-08 19:54:06 +00:00
|
|
|
that in most scenarios jQuery is already loaded.
|
2012-09-14 06:34:36 +00:00
|
|
|
|
|
|
|
|
Example Application
|
|
|
|
|
-------------------
|
2015-09-25 20:11:50 +00:00
|
|
|
Please see ``tests/testapp`` application.
|
|
|
|
|
This application is used to manually test the functionalities of this package.
|
|
|
|
|
This also serves as a good example.
|