# Custom Content Display ## Example Overview - **Objective**: This example showcases how to customize the display of content in the page based on selected items using `django_tomselect`. - **Features Highlighted**: - Custom rendering of page content based on selected dropdown items. **Visual Examples** ![Screenshot: Custom Option Display](https://raw.githubusercontent.com/OmenApps/django-tomselect/refs/heads/main/docs/images/custom-content.png) ## Key Code Segments ### Forms The form for selecting embargo regions and timeframes uses `TomSelectModelChoiceField` and `TomSelectChoiceField` to render the dropdowns. :::{admonition} Form Definition :class: dropdown ```python class EmbargoForm(forms.Form): """Form for selecting embargo regions and timeframes.""" region = TomSelectModelChoiceField( config=TomSelectConfig( url="autocomplete-embargo-region", value_field="id", label_field="name", placeholder="Select region...", highlight=True, preload="focus", plugin_dropdown_header=PluginDropdownHeader( title="Publishing Regions", extra_columns={ "market_tier": "Market Tier", "typical_embargo_days": "Typical Days", }, ), ) ) timeframe = TomSelectChoiceField( config=TomSelectConfig( url="autocomplete-embargo-timeframe", value_field="value", label_field="label", placeholder="Select timeframe...", highlight=True, ) ) ``` ::: ### Templates The template for the content embargo management page uses custom CSS classes to style additional information based on the selected region. :::{admonition} Template Code :class: dropdown ```html {% extends 'example/base_with_bootstrap5.html' %} {% block extra_header %} {{ form.media }} {% endblock %} {% block content %}

Content Embargo Management

This example demonstrates implement cascading selects with rich metadata, displaying custom information via JavaScript based on selected options, and the use of both model- and iterator-based form fields in a single form.


Configure content embargo periods for different publishing regions. Each region has specific market requirements and typical embargo periods based on their tier and local regulations.

{% csrf_token %}
{{ form.region }}
{{ form.timeframe }}
{% endblock %} ``` ::: ### Autocomplete Views The `autocomplete-enriched-content` endpoint provides the necessary data for the display. Here we override the `hook_prepare_results` method to format the response with additional fields. :::{admonition} Autocomplete View :class: dropdown ```python class EmbargoRegionAutocompleteView(AutocompleteModelView): """Autocomplete view for embargo regions.""" model = EmbargoRegion search_lookups = ["name__icontains"] value_fields = [ "id", "name", "market_tier", "typical_embargo_days", "content_restrictions", ] skip_authorization = True def hook_prepare_results(self, results: list[dict[str, Any]]) -> list[dict[str, Any]]: """Format results with tier information and restrictions.""" formatted_results = [] for region in results: formatted_results.append( { "id": region["id"], "name": str(region["name"]), "market_tier": f"Tier {region['market_tier']}", "typical_embargo_days": region["typical_embargo_days"], "content_restrictions": region["content_restrictions"], } ) return formatted_results ``` ::: ## Views The view for managing embargoes processes the form data and displays a success message with the selected region and timeframe. :::{admonition} View Code :class: dropdown ```python def embargo_management_view(request): """View for managing embargoes.""" template = "example/intermediate_demos/embargo_management.html" context = {} form = EmbargoForm() if request.method == "POST": form = EmbargoForm(request.POST) if form.is_valid(): region = form.cleaned_data["region"] timeframe = form.cleaned_data["timeframe"] def get_timeframe_display(timeframe): """Get the display value for the timeframe.""" return dict(EmbargoTimeframe.choices)[timeframe] messages.success( request, f"Embargo for {region} set to {get_timeframe_display(timeframe)}.", ) return redirect("custom-content") context["form"] = form return TemplateResponse(request, template, context) ``` :::