Styling Demos

Example Overview

This example demonstrates the use of django_tomselect with Default, Bootstrap 4, and Bootstrap 5 styling to create responsive, dynamic <select> fields. The example includes various configurations for single and multiple selections, highlighting the integration with a modern CSS framework.

What problem does it solve?

This setup simplifies the creation of user-friendly, visually appealing dropdowns with advanced features like:

  • Autocomplete.

  • Placeholder text.

  • Tabular dropdown headers.

  • Options for creating and managing items directly from the dropdown.

Key features highlighted:

  • Single and multiple select fields with Bootstrap 4 styling.

  • Server-side autocomplete with configurable behavior.

Visual Examples

Screenshot: Single Selection Screenshot: Multiple Selection with Tabular Display


Key Code Segments

Forms

The DefaultStylingForm, Bootstrap4StylingForm, and Bootstrap5StylingForm in forms/basic_demos.py configures fields using TomSelectConfig for the prefered styling.

  • Purpose: This sets up a dropdown with an autocomplete endpoint (url), styling with preferred style, and a clear button plugin.


Template

The default.html. bs4.html, and bs5.html templates render the form using the specified styling. They extend the base layout and include the necessary CSS and JavaScript assets.


Autocomplete View

The autocomplete functionality is defined in the autocompletes.py file using AutocompleteModelView.

  • Here, we use skip_authorization to bypass the default authorization checks for demonstration purposes.

  • Any searches will use the name field of the Edition model.

  • The page_size parameter sets the number of results to return per page.

  • The value_fields parameter specifies the fields to return in the response.

  • The list_url, create_url, update_url, and delete_url parameters are the url names for the respective views, if available.

  • Purpose: Provides the backend logic for fetching autocomplete results, with filtering and ordering.


Views

We use standard Django views to render the form.


Design and Implementation Notes

Key Features

  • Dynamic styling: Uses the preferred styling.

  • Plugins: Includes plugins like clear_button and dropdown_footer to enhance UX.

Alternative Approaches

  • Use the “default” styling if Bootstrap is not integrated into the project.

  • Use additional plugins like dropdown_footer to add create and list buttons directly in the dropdown.

Potential Extensions

  • Add dependent dropdowns where one field’s options are filtered based on another field’s selection.