A suitable Django autocomplete widget using web components
Project description
Suitable Django Autocomplete
A modern, accessible autocomplete widget for Django 4.2+ built with web components. No jQuery or other JavaScript framework dependencies required.
Features
- 🎯 Zero dependencies - No jQuery, no framework lock-in
- ♿ Fully accessible - ARIA compliant with keyboard navigation
- 🚀 Web components - Uses native browser APIs
- 🔍 Smart search - Debounced search with loading states
- 🎨 Customizable - Easy to style and extend
- 📱 Mobile friendly - Works great on touch devices
- 🔧 Django integration - Seamless form field integration
Installation
pip install suitable-django-autocomplete
Or with uv:
uv add suitable-django-autocomplete
Quick Start
1. Add to your Django settings
INSTALLED_APPS = [
...
'suitable_django_autocomplete',
]
2. Create an autocomplete view
# views.py
from suitable_django_autocomplete import SimpleAutocompleteView
class FruitAutocompleteView(SimpleAutocompleteView):
choices = [
'Apple', 'Apricot', 'Banana', 'Blackberry', 'Blueberry',
'Cherry', 'Date', 'Elderberry', 'Fig', 'Grape'
]
3. Add URL pattern
# urls.py
from django.urls import path
from .views import FruitAutocompleteView
urlpatterns = [
path('autocomplete/fruits/', FruitAutocompleteView.as_view(), name='fruits-autocomplete'),
]
4. Use in your forms
# forms.py
from django import forms
from suitable_django_autocomplete import AutocompleteField
class OrderForm(forms.Form):
favorite_fruit = AutocompleteField(
url='/autocomplete/fruits/',
attrs={'placeholder': 'Start typing a fruit name...'}
)
5. Include in your templates
<!-- your_template.html -->
<form method="post">
{% csrf_token %} {{ form.as_p }}
<!-- This loads the required CSS and JavaScript -->
{{ form.media }}
<button type="submit">Submit</button>
</form>
Model-Based Autocomplete
For database-backed autocomplete, use ModelAutocompleteView:
# views.py
from django.contrib.auth.models import User
from suitable_django_autocomplete import ModelAutocompleteView
class UserAutocompleteView(ModelAutocompleteView):
model = User
search_fields = ['username', 'first_name', 'last_name', 'email']
def get_queryset(self):
# Optional: add custom filtering
return super().get_queryset().filter(is_active=True)
# forms.py
from suitable_django_autocomplete import ModelAutocompleteField
class TaskForm(forms.ModelForm):
assigned_to = ModelAutocompleteField(
queryset=User.objects.filter(is_active=True),
url='/autocomplete/users/',
attrs={'placeholder': 'Search for a user...'}
)
class Meta:
model = Task
fields = ['title', 'assigned_to']
Advanced Usage
Custom Search Logic
class ProductAutocompleteView(ModelAutocompleteView):
model = Product
search_fields = ['name', 'sku', 'category__name']
def get_queryset(self):
qs = super().get_queryset()
# Only show products in stock
return qs.filter(stock__gt=0)
def get_result_label(self, obj):
# Customize how results are displayed
return f"{obj.name} (SKU: {obj.sku})"
Styling
The widget uses semantic HTML and can be styled with CSS:
/* Custom styling example */
.autocomplete-container {
position: relative;
width: 100%;
}
.autocomplete-input {
width: 100%;
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
}
.autocomplete-results {
position: absolute;
top: 100%;
left: 0;
right: 0;
background: white;
border: 1px solid #ddd;
border-top: none;
max-height: 200px;
overflow-y: auto;
z-index: 1000;
}
.autocomplete-result {
padding: 8px 12px;
cursor: pointer;
}
.autocomplete-result:hover,
.autocomplete-result[aria-selected="true"] {
background-color: #f0f0f0;
}
Browser Support
- Chrome 61+
- Firefox 63+
- Safari 10.1+
- Edge 79+
Requirements
- Python 3.9+
- Django 4.2+
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file suitable_django_autocomplete-0.2.0.tar.gz.
File metadata
- Download URL: suitable_django_autocomplete-0.2.0.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54407cc1b03c050d3c2c9f4ed54a740d805e6e6ff67432ef61d0da9380b9d66e
|
|
| MD5 |
a9529121a03c037507ccd05a879cb5e2
|
|
| BLAKE2b-256 |
0bb1b61c33a7e0b2d5b512428160e7541c586a27cd9fed4c43a427587be2d0fb
|
Provenance
The following attestation bundles were made for suitable_django_autocomplete-0.2.0.tar.gz:
Publisher:
publish.yml on suitable-adventures/suitable-django-autocomplete
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
suitable_django_autocomplete-0.2.0.tar.gz -
Subject digest:
54407cc1b03c050d3c2c9f4ed54a740d805e6e6ff67432ef61d0da9380b9d66e - Sigstore transparency entry: 242992829
- Sigstore integration time:
-
Permalink:
suitable-adventures/suitable-django-autocomplete@9c611ceee4e26086d1ca57cd42cc55fdee2945a2 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/suitable-adventures
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9c611ceee4e26086d1ca57cd42cc55fdee2945a2 -
Trigger Event:
push
-
Statement type:
File details
Details for the file suitable_django_autocomplete-0.2.0-py3-none-any.whl.
File metadata
- Download URL: suitable_django_autocomplete-0.2.0-py3-none-any.whl
- Upload date:
- Size: 14.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87f633774452225291ba91bf6d298e61743644c1a80f04024022b3be0fe2091f
|
|
| MD5 |
571f04f51bb307606fbcaf25e4a27e28
|
|
| BLAKE2b-256 |
0f1990432d9458cb1c96fbb224cbad36b65de26890fcf38c60c1b0624a2b60af
|
Provenance
The following attestation bundles were made for suitable_django_autocomplete-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on suitable-adventures/suitable-django-autocomplete
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
suitable_django_autocomplete-0.2.0-py3-none-any.whl -
Subject digest:
87f633774452225291ba91bf6d298e61743644c1a80f04024022b3be0fe2091f - Sigstore transparency entry: 242992831
- Sigstore integration time:
-
Permalink:
suitable-adventures/suitable-django-autocomplete@9c611ceee4e26086d1ca57cd42cc55fdee2945a2 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/suitable-adventures
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9c611ceee4e26086d1ca57cd42cc55fdee2945a2 -
Trigger Event:
push
-
Statement type: