Skip to content

Commit cc952e1

Browse files
authored
Update documentation and readme (#360)
1 parent 4529d6c commit cc952e1

15 files changed

+399
-340
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ repos:
3939
rev: v0.20.0
4040
hooks:
4141
- id: yamlfmt
42+
- repo: https://github.com/sphinx-contrib/sphinx-lint
43+
rev: v1.0.2
44+
hooks:
45+
- id: sphinx-lint
4246
ci:
4347
autoupdate_schedule: weekly
4448
skip:

CONTRIBUTING.rst

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
1-
Contributing
2-
============
1+
##############
2+
Contributing
3+
##############
34

4-
Before you start editing the python code, you will need to make sure
5-
you have binary dependencies installed::
5+
Before you start editing the python code, you will need to make sure you
6+
have binary dependencies installed:
67

7-
# Debian
8-
sudo apt install -y gettext graphviz google-chrome-stable
9-
# macOS
10-
brew install -y gettext graphviz google-chrome-stable
8+
.. code::
119
12-
You may run the tests via::
10+
# Debian
11+
sudo apt install -y gettext graphviz google-chrome-stable
12+
# macOS
13+
brew install -y gettext graphviz google-chrome-stable
1314
14-
uv run pytest
15+
You may run the tests via:
1516

16-
Documentation pull requests welcome. The Sphinx documentation can be compiled via::
17+
.. code::
1718
18-
uv run sphinx-build -W -b doctest -b html docs docs/_build/html
19+
uv run pytest
1920
20-
Bug reports welcome, even more so if they include a correct patch. Much
21-
more so if you start your patch by adding a failing unit test, and correct
22-
the code until zero unit tests fail.
21+
Documentation pull requests welcome. The Sphinx documentation can be
22+
compiled via:
2323

24-
The list of supported Django and Python version can be found in the CI suite setup.
25-
Please make sure to verify that none of the linters or tests failed, before you submit
26-
a patch for review.
24+
.. code::
25+
26+
uv run sphinx-build -W -b doctest -b html docs docs/_build/html
27+
28+
Bug reports welcome, even more so if they include a correct patch. Much
29+
more so if you start your patch by adding a failing unit test, and
30+
correct the code until zero unit tests fail.
31+
32+
The list of supported Django and Python version can be found in the CI
33+
suite setup. Please make sure to verify that none of the linters or
34+
tests failed, before you submit a patch for review.

MANIFEST.in

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

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<p align="center">
2+
<picture>
3+
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/codingjoe/django-select2/raw/main/images/logo-dark.svg">
4+
<source media="(prefers-color-scheme: light)" srcset="https://github.com/codingjoe/django-select2/raw/main/images/logo-light.svg">
5+
<img alt="Django Select2: Custom autocomplete fields for Django" src="https://github.com/codingjoe/django-select2/raw/main/images/logo-light.svg">
6+
</picture>
7+
<br>
8+
<a href="https://django-select2.rtfd.io">Documentation</a>
9+
</p>
10+
11+
# Django-Select2
12+
13+
[![version](https://img.shields.io/pypi/v/Django-Select2.svg)](https://pypi.python.org/pypi/Django-Select2/)
14+
[![coverage](https://codecov.io/gh/codingjoe/django-select2/branch/main/graph/badge.svg)](https://codecov.io/gh/codingjoe/django-select2)
15+
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/codingjoe/django-select2/main/LICENSE.txt)
16+
17+
Custom autocomplete fields for [Django](https://www.djangoproject.com/).
18+
19+
## Documentation
20+
21+
Documentation available at <https://django-select2.rtfd.io>.
22+
23+
> [!NOTE]
24+
> Django's admin comes with builtin support for Select2 via the [autocomplete_fields](https://docs.djangoproject.com/en/stable/ref/contrib/admin/#django.contrib.admin.ModelAdmin.autocomplete_fields) feature.

README.rst

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

SECURITY.md

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

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
version = ".".join(release.split(".")[:2])
2525

2626
master_doc = "index" # default in Sphinx v2
27+
html_theme = "furo"
2728

2829

2930
extensions = [

docs/django_select2.rst

Lines changed: 103 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,140 @@
1-
API Documentation
2-
=================
1+
###################
2+
API Documentation
3+
###################
34

4-
Configuration
5-
-------------
5+
***************
6+
Configuration
7+
***************
68

79
.. automodule:: django_select2.conf
8-
:members:
9-
:undoc-members:
10-
:show-inheritance:
10+
:members:
11+
:undoc-members:
12+
:show-inheritance:
1113

12-
Widgets
13-
-------
14+
*********
15+
Widgets
16+
*********
1417

1518
.. automodule:: django_select2.forms
16-
:members:
17-
:undoc-members:
18-
:show-inheritance:
19+
:members:
20+
:undoc-members:
21+
:show-inheritance:
1922

20-
URLs
21-
----
23+
******
24+
URLs
25+
******
2226

2327
.. automodule:: django_select2.urls
24-
:members:
25-
:undoc-members:
26-
:show-inheritance:
28+
:members:
29+
:undoc-members:
30+
:show-inheritance:
2731

28-
Views
29-
-----
32+
*******
33+
Views
34+
*******
3035

3136
.. automodule:: django_select2.views
32-
:members:
33-
:undoc-members:
34-
:show-inheritance:
37+
:members:
38+
:undoc-members:
39+
:show-inheritance:
3540

36-
Cache
37-
-----
41+
*******
42+
Cache
43+
*******
3844

3945
.. automodule:: django_select2.cache
40-
:members:
41-
:undoc-members:
42-
:show-inheritance:
46+
:members:
47+
:undoc-members:
48+
:show-inheritance:
4349

50+
************
51+
JavaScript
52+
************
4453

45-
JavaScript
46-
----------
54+
DjangoSelect2 handles the initialization of select2 fields
55+
automatically. Just include ``{{ form.media.js }}`` in your template
56+
before the closing ``body`` tag. That's it!
4757

48-
DjangoSelect2 handles the initialization of select2 fields automatically. Just include
49-
``{{ form.media.js }}`` in your template before the closing ``body`` tag. That's it!
58+
If you insert forms after page load or if you want to handle the
59+
initialization yourself, DjangoSelect2 provides a jQuery plugin,
60+
replacing and enhancing the Select2 plugin. It will handle both normal
61+
and heavy fields. Simply call ``djangoSelect2(options)`` on your select
62+
fields.:
5063

51-
If you insert forms after page load or if you want to handle the initialization
52-
yourself, DjangoSelect2 provides a jQuery plugin, replacing and enhancing the Select2
53-
plugin. It will handle both normal and heavy fields. Simply call
54-
``djangoSelect2(options)`` on your select fields.::
64+
.. code::
5565
56-
$('.django-select2').djangoSelect2();
66+
$('.django-select2').djangoSelect2();
5767
5868
Please replace all your ``.select2`` invocations with the here provided
5969
``.djangoSelect2``.
6070

71+
*********************
72+
Configuring Select2
73+
*********************
6174

62-
Configuring Select2
63-
-------------------
64-
65-
Select2 options can be configured either directly from Javascript or from within Django
66-
using widget attributes. `(List of options in the Select2 docs) <https://select2.org/configuration/options-api>`_.
75+
Select2 options can be configured either directly from Javascript or
76+
from within Django using widget attributes. `(List of options in the
77+
Select2 docs) <https://select2.org/configuration/options-api>`_.
6778

6879
To pass options in javascript
6980

70-
.. code-block:: javascript
71-
72-
$('.django-select2').djangoSelect2({
73-
minimumInputLength: 0,
74-
placeholder: 'Select an option',
75-
});
76-
77-
From Django, you can use ``data-`` attributes using the same names in camel-case and
78-
passing them to your widget. Select2 will then pick these up. For example when
79-
initialising a widget in a form, you could do:
80-
81-
.. code-block:: python
82-
83-
class MyForm(forms.Form):
84-
my_field = forms.ModelMultipleChoiceField(
85-
widget=ModelSelect2MultipleWidget(
86-
model=MyModel
87-
search_fields=['another_field']
88-
attrs={
89-
"data-minimum-input-length": 0,
90-
"data-placeholder": "Select an option",
91-
"data-close-on-select": "false",
92-
}
93-
)
94-
)
95-
96-
(If you do not want to initialize the widget, you could add the attributes by overriding
97-
a widget method and adding them in a super call, e.g. `get_context() <https://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Widget.get_context>`_)
98-
99-
100-
Security & Authentication
101-
-------------------------
81+
.. code:: javascript
82+
83+
$('.django-select2').djangoSelect2({
84+
minimumInputLength: 0,
85+
placeholder: 'Select an option',
86+
});
87+
88+
From Django, you can use ``data-`` attributes using the same names in
89+
camel-case and passing them to your widget. Select2 will then pick these
90+
up. For example when initialising a widget in a form, you could do:
91+
92+
.. code:: python
93+
94+
class MyForm(forms.Form):
95+
my_field = forms.ModelMultipleChoiceField(
96+
widget=ModelSelect2MultipleWidget(
97+
model=MyModel
98+
search_fields=['another_field']
99+
attrs={
100+
"data-minimum-input-length": 0,
101+
"data-placeholder": "Select an option",
102+
"data-close-on-select": "false",
103+
}
104+
)
105+
)
106+
107+
(If you do not want to initialize the widget, you could add the
108+
attributes by overriding a widget method and adding them in a super
109+
call, e.g. `get_context()
110+
<https://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Widget.get_context>`_)
111+
112+
***************************
113+
Security & Authentication
114+
***************************
102115

103116
Security is important. Therefore make sure to read and understand what
104117
the security measures in place and their limitations.
105118

106-
Set up a separate cache. If you have a public form that uses a model widget
107-
make sure to setup a separate cache database for Select2. An attacker
108-
could constantly reload your site and fill up the select2 cache.
109-
Having a separate cache allows you to limit the effect to select2 only.
119+
Set up a separate cache. If you have a public form that uses a model
120+
widget make sure to setup a separate cache database for Select2. An
121+
attacker could constantly reload your site and fill up the select2
122+
cache. Having a separate cache allows you to limit the effect to select2
123+
only.
110124

111125
You might want to add a secure select2 JSON endpoint for data you don't
112-
want to be accessible to the general public. Doing so is easy::
126+
want to be accessible to the general public. Doing so is easy:
127+
128+
.. code::
113129
114-
class UserSelect2View(LoginRequiredMixin, AutoResponseView):
115-
pass
130+
class UserSelect2View(LoginRequiredMixin, AutoResponseView):
131+
pass
116132
117-
class UserSelect2WidgetMixin(object):
118-
def __init__(self, *args, **kwargs):
119-
kwargs['data_view'] = 'user-select2-view'
120-
super(UserSelect2WidgetMixin, self).__init__(*args, **kwargs)
133+
class UserSelect2WidgetMixin(object):
134+
def __init__(self, *args, **kwargs):
135+
kwargs['data_view'] = 'user-select2-view'
136+
super(UserSelect2WidgetMixin, self).__init__(*args, **kwargs)
121137
122-
class MySecretWidget(UserSelect2WidgetMixin, ModelSelect2Widget):
123-
model = MySecretModel
124-
search_fields = ['title__icontains']
138+
class MySecretWidget(UserSelect2WidgetMixin, ModelSelect2Widget):
139+
model = MySecretModel
140+
search_fields = ['title__icontains']

0 commit comments

Comments
 (0)