Skip to main content

django-selectize is a Django app based on Selectize.js that help you to create Select and Multiselect widgets in Django forms.

Project description

Django Selectize

django-selectize is a Django app based on Selectize.js that help you to create Select and Multiselect widgets in Django forms.

Installation

Install django-selectize:

pip install django-selectize

Add django-selectize to your INSTALLED_APPS in your project settings.

INSTALLED_APPS = [
    # other django apps...
    'django_selectize',
]

Quick Start

Here is a quick example to get you started:

We have the following model:

# models.py
from django.conf import settings
from django.db import models

class Book(models.Model):
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    co_authors = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='co_authored_by')

Next, we create a model form with custom Selectize widgets.

# forms.py
from django import forms
from django_selectize import forms as s2forms

from . import models

class AuthorWidget(s2forms.SelectizeWidget):
	search_fields = [
		"username__icontains",
		"email__icontains",
	]

class CoAuthorsWidget(s2forms.SelectizeMultipleWidget):
	search_fields = [
		"username__icontains",
		"email__icontains",
	]

class BookForm(forms.ModelForm):
	class Meta:
		model = models.Book
		fields = "__all__"
		widgets = {
			"author": AuthorWidget,
			"co_authors": CoAuthorsWidget,
		}

A simple class based view will do, to render your form:

# views.py
from django.views import generic

from . import forms, models

class BookCreateView(generic.CreateView):
    model = models.Book
    form_class = forms.BookForm
    success_url = "/"

Make sure to add the view to your urls.py:

# urls.py
from django.urls import include, path
from . import views

urlpatterns = [
    # ... other patterns
    path("", views.BookCreateView.as_view(), name="book-create"),
]

Finally, we need a little template, myapp/templates/myapp/book_form.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Create Book</title>
    {{ form.media.css }}
    <style>
        input, select {width: 100%}
    </style>
</head>
<body>
    <h1>Create a new Book</h1>
    <form method="POST">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit">
    </form>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    {{ form.media.js }}
</body>
</html>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-selectize-2.0.tar.gz (123.9 kB view details)

Uploaded Source

Built Distribution

django_selectize-2.0-py3-none-any.whl (123.8 kB view details)

Uploaded Python 3

File details

Details for the file django-selectize-2.0.tar.gz.

File metadata

  • Download URL: django-selectize-2.0.tar.gz
  • Upload date:
  • Size: 123.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for django-selectize-2.0.tar.gz
Algorithm Hash digest
SHA256 5a7c43fa2f66fb8c3826f10c3684c57ce07d5bd747d330393d947c95a3710077
MD5 263d9f978f871bfeccef2c60c7c36a52
BLAKE2b-256 e92b878ffd9269599bab113c5ad6416b04a1808a1e6c8c4d041c371cb4075dd8

See more details on using hashes here.

File details

Details for the file django_selectize-2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_selectize-2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb50dca21db368e2484ffd1b96dc588fd8efe129fcb66a7572f9e4f9ef3db7a5
MD5 5ad451c6d5da68b7e383c5ce1406122a
BLAKE2b-256 6fee7a80cd3fecc4417ddcd1a99c815f2bdd14b96e68f92de16e1da89f581cd2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page