diff --git a/.vscode/settings.json b/.vscode/settings.json index d69ce88..703353b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,11 +1,15 @@ { - "markdown-preview-github-styles.colorTheme": "light", "editor.rulers": [ 88 ], - "python.linting.pylintEnabled": true, - "python.linting.enabled": true, - "python.formatting.provider": "black", + "[python]": { + "editor.defaultFormatter": "ms-python.black-formatter", + "editor.tabSize": 4 + }, + "editor.formatOnSave": true, + "editor.codeActionsOnSave": { + "source.organizeImports": "explicit" + }, "cSpell.words": [ "htmx", "textinput" diff --git a/README.md b/README.md index 30c335a..da9df57 100644 --- a/README.md +++ b/README.md @@ -1,135 +1,230 @@ # django-htmx-autocomplete -This Django app provides a client-side autocomplete component powered by +This Django app provides an autocomplete widiget component powered by [htmx](https://htmx.org/) featuring multiselect, search and is completely extensible. - ## Quick start 1. Add "autocomplete" to your `INSTALLED_APPS` setting like this: - ```python - # settings.py - INSTALLED_APPS = [ - ... - 'django.contrib.staticfiles', # also required - 'autocomplete', - ] - ``` + ```python + # settings.py + INSTALLED_APPS = [ + ... + 'django.contrib.staticfiles', # also required + 'autocomplete', + ] + ``` 1. Include the autocomplete urls like this: - ```python - # urls.py - ... - from autocomplete import HTMXAutoComplete - - urlpatterns = [ - ... - *HTMXAutoComplete.url_dispatcher('ac'), - ] - ``` - - This will add routes prefixed by `ac` to support component instances. - -1. Use either the widget or class to create components! - - ```python - from django forms - from django.db import models - from autocomplete import HTMXAutoComplete, widgets - - # Example models - class Person(models.Model): - name = models.CharField(max_length=60) - - class Team(models.Model): - name = models.CharField(max_length=60) - members = models.ManyToManyField(Person) - - # Using the widget - class MultipleFormModel(forms.ModelForm): - """Multiple select example form using a model""" - class Meta: - """Meta class that configures the form""" - model = Team - fields = ['name', 'members'] - widgets = { - 'members': widgets.Autocomplete( - name='members', - options=dict(multiselect=True, model=Person) - ) - } - - # Using the class - class GetItemsMultiAutoComplete(HTMXAutoComplete): - name = "members" - multiselect = True - - class Meta: - model = Person - - ``` + ```python + # urls.py + ... + from autocomplete import urls as autocomplete_urls + + urlpatterns = [ + # ... + path("ac/", autocomplete_urls), + ] + ``` + +1. Create an autocomplete class that extends `autocomplete.ModelAutocomplete`, + + ```python + from django forms + from django.db import models + from autocomplete import Autocomplete, AutocompleteWidget + + class Person(models.Model): + name = models.CharField(max_length=60) + + class Team(models.Model): + team_lead = models.ForeignKey( + Person, null=True, on_delete=models.SET_NULL, related_name="lead_teams" + ) + + members = models.ManyToManyField(Person) + + class PersonAutocomplete(ModelAutocomplete): + model = Person + search_attrs = [ 'name' ] + + + class MultipleFormModel(forms.ModelForm): + """Multiple select example form using a model""" + class Meta: + """Meta class that configures the form""" + model = Team + fields = ['team_lead', 'members'] + widgets = { + 'team_lead': AutocompleteWidget( + ac_class=PersonAutocomplete, + ), + 'members': AutocompleteWidget( + ac_class=PersonAutocomplete, + options={"multiselect": True}, + ) + } + ``` 1. Make sure your templates include HTMX. > **Note** > Bootstrap is included in this example styling, however it is not required. - ```django - {% load autocomplete %} - {% load static %} - - -
- - - - -