Skip to content

Commit d59288f

Browse files
committed
Add more comprehencive gettings started app and example
1 parent ed0578d commit d59288f

File tree

19 files changed

+517
-136
lines changed

19 files changed

+517
-136
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ ghostdriver.log
2323

2424
coverage.xml
2525
.eggs/
26+
db.sqlite3

README.rst

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,21 @@
1+
==============
12
Django-Select2
23
==============
34

4-
|version| |ci| |coverage| |license|
5-
65
This is a `Django`_ integration of `Select2`_.
76

87
The app includes Select2 driven Django Widgets.
98

10-
.. note::
11-
Django's admin comes with builtin support for Select2
12-
since version 2.0 via the `autocomplete_fields`_ feature.
13-
14-
Installation
15-
------------
16-
17-
1. Install ``django_select2``
18-
19-
.. code:: python
20-
21-
pip install django_select2
22-
23-
2. Add ``django_select2`` to your ``INSTALLED_APPS`` in your project
24-
settings.
25-
26-
3. Add ``django_select`` to your urlconf if you use any "Auto" fields.
27-
28-
.. code:: python
29-
30-
url(http://github.com/%3Cspan%20class=%22pl-sr%22%3E%3Cspan%20class=%22pl-k%22%3Er%3C/span%3E%3Cspan%20class=%22pl-pds%22%3E'%3C/span%3E%3Cspan%20class=%22pl-c1%22%3E^%3C/span%3Eselect2/%3Cspan%20class=%22pl-pds%22%3E'%3C/span%3E%3C/span%3E,%20include(%3Cspan%20class=%22pl-s%22%3E%3Cspan%20class=%22pl-pds%22%3E'%3C/span%3Edjango_select2.urls%3Cspan%20class=%22pl-pds%22%3E'%3C/span%3E%3C/span%3E)),
31-
329
Documentation
3310
-------------
3411

3512
Documentation available at https://django-select2.readthedocs.io/.
3613

37-
External Dependencies
38-
---------------------
39-
40-
- jQuery version 2 This is not included in the package since it is
41-
expected that in most scenarios this would already be available.
42-
43-
Example Application
44-
-------------------
45-
46-
Please see ``tests/testapp`` application. This application is used to
47-
manually test the functionalities of this package. This also serves as a
48-
good example.
49-
50-
Changelog
51-
---------
52-
53-
See `Github releases`_
14+
.. note::
15+
Django's admin comes with builtin support for Select2
16+
via the `autocomplete_fields`_ feature.
5417

5518

5619
.. _Django: https://www.djangoproject.com/
5720
.. _Select2: https://select2.org/
5821
.. _autocomplete_fields: https://docs.djangoproject.com/en/stable/ref/contrib/admin/#django.contrib.admin.ModelAdmin.autocomplete_fields
59-
.. _CHANGELOG.md: CHANGELOG.md
60-
.. _Github releases: https://github.com/applegrew/django-select2/releases
61-
62-
.. |version| image:: https://img.shields.io/pypi/v/Django-Select2.svg
63-
:target: https://pypi.python.org/pypi/Django-Select2/
64-
.. |ci| image:: https://travis-ci.org/applegrew/django-select2.svg?branch=master
65-
:target: https://travis-ci.org/applegrew/django-select2
66-
.. |coverage| image:: https://codecov.io/gh/applegrew/django-select2/branch/master/graph/badge.svg
67-
:target: https://codecov.io/gh/applegrew/django-select2
68-
.. |license| image:: https://img.shields.io/badge/license-APL2-blue.svg
69-
:target: https://raw.githubusercontent.com/applegrew/django-select2/master/LICENSE.txt

docs/get_started.rst

Lines changed: 0 additions & 78 deletions
This file was deleted.

docs/index.rst

Lines changed: 183 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,186 @@
1-
.. Django-Select2 documentation master file, created by
2-
sphinx-quickstart on Sat Aug 25 10:23:46 2012.
3-
You can adapt this file completely to your liking, but it should at least
4-
contain the root `toctree` directive.
1+
.. include:: ../README.rst
2+
3+
Installation
4+
------------
5+
6+
Install ``django-select2``
7+
8+
.. code-block:: python
9+
10+
python3 -m pip install django-select2
11+
12+
Add ``django_select2`` to your ``INSTALLED_APPS`` in your project settings.
13+
14+
Add ``django_select`` to your urlconf if you use any "Auto" fields.
15+
16+
.. code-block:: python
17+
18+
from django.urls import include, path
19+
20+
urlpatterns = [
21+
# … other patterns
22+
path("select2/", include("django_select2.urls")),
23+
# … other patterns
24+
]
25+
26+
Finally make sure you have a persistent cache backend setup
27+
(not the dummy backen), we will use Redis in this example.
28+
Make sure you have a Redis server up and running::
29+
30+
# Debian
31+
sudo apt-get install redis-server
32+
# macOS
33+
brew install redis
34+
# install Redis python client
35+
python3 -m pip install django-redis
36+
37+
Next, add the cache configuration to your ``settings.py`` as follows:
38+
39+
.. code-block:: python
40+
41+
CACHES = {
42+
# … default cache config and others
43+
"select2": {
44+
"BACKEND": "django_redis.cache.RedisCache",
45+
"LOCATION": "redis://127.0.0.1:6379/2",
46+
"OPTIONS": {
47+
"CLIENT_CLASS": "django_redis.client.DefaultClient",
48+
}
49+
}
50+
}
51+
52+
# Tell select2 which cache configuration to use:
53+
SELECT2_CACHE_BACKEND = "select2"
54+
55+
56+
External Dependencies
57+
---------------------
58+
59+
- jQuery is not included in the package since it is
60+
expected that in most scenarios this would already be available.
61+
62+
63+
Quick Start
64+
-----------
65+
66+
Here is a quick example to get you started:
67+
68+
First make sure you followed the installation instructions above.
69+
Once everything is setup, let's start with a simple example.
70+
71+
We have the following model:
72+
73+
.. code-block:: python
74+
75+
# models.py
76+
from django.conf import settings
77+
from django.db import models
78+
79+
80+
class Book(models.Model):
81+
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
82+
co_authors = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='co_authors')
83+
84+
85+
Replace native Django forms widgets with one of the several ``django_select2.form`` widgets.
86+
Start by importing them into your ``forms.py``, right next to Django own ones:
87+
88+
.. code-block:: python
89+
90+
# forms.py
91+
from django import forms
92+
from django_select2 import forms as s2forms
93+
94+
from . import models
95+
96+
97+
class AuthorWidget(s2forms.ModelSelect2Widget):
98+
search_fields = [
99+
"username__icontains",
100+
"email__icontains",
101+
]
102+
103+
104+
class CoAuthorWidget(s2forms.ModelSelect2MultipleWidget):
105+
search_fields = [
106+
"username__icontains",
107+
"email__icontains",
108+
]
109+
110+
111+
class BookForm(forms.ModelForm):
112+
class Meta:
113+
model = models.Book
114+
fields = "__all__"
115+
widgets = {
116+
"author": AuthorWidget,
117+
"co_authors": CoAuthorWidget,
118+
}
119+
120+
A simple class based view will do, to render your form:
121+
122+
.. code-block:: python
123+
124+
# views.py
125+
from django.views import generic
126+
127+
from . import forms, models
128+
129+
130+
class BookCreateView(generic.CreateView):
131+
model = models.Book
132+
form_class = forms.BookForm
133+
134+
Make sure to add the view to your ``urls.py``:
135+
136+
.. code-block:: python
137+
138+
# urls.py
139+
from django.urls import include, path
140+
141+
from . import views
142+
143+
urlpatterns = [
144+
# … other patterns
145+
path("select2/", include("django_select2.urls")),
146+
# … other patterns
147+
path("book/create", views.BookCreateView.as_view(), name="book-create"),
148+
]
149+
150+
151+
Finally, we need a little template, ``myapp/templates/myapp/book_form.html``
152+
153+
.. code-block:: HTML
154+
155+
<!DOCTYPE html>
156+
<html lang="en">
157+
<head>
158+
<title>Create Book</title>
159+
{{ form.media.css }}
160+
<style>
161+
input, select {width: 100%}
162+
</style>
163+
</head>
164+
<body>
165+
<h1>Create a new Book</h1>
166+
<form action="POST">
167+
{{ form.as_p }}
168+
<input type="submit">
169+
</form>
170+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
171+
{{ form.media.js }}
172+
</body>
173+
</html>
174+
175+
Done - enjoy the wonders of Select2!
176+
177+
178+
Changelog
179+
---------
180+
181+
See `Github releases`_.
182+
183+
.. _Github releases: https://github.com/applegrew/django-select2/releases
5184

6185
All Contents
7186
============
@@ -12,7 +191,6 @@ Contents:
12191
:maxdepth: 2
13192
:glob:
14193

15-
get_started
16194
django_select2
17195
extra
18196
CONTRIBUTING

example/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Sample App
2+
3+
To run the sample app, please run:
4+
5+
```
6+
git clone https://github.com/applegrew/django-select2.git
7+
cd django-select2/example
8+
python3 -m pip install -r requirements.txt
9+
python3 manage.py migrate
10+
python3 manage.py createsuperuser
11+
# follow the instructions to create a superuser
12+
python3 manage.py runserver
13+
# follow the instructions and open your browser
14+
```

example/example/__init__.py

Whitespace-only changes.

example/example/asgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for example project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/dev/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'example.settings')
15+
16+
application = get_asgi_application()

0 commit comments

Comments
 (0)