Skip to main content

Form widgets

Added in 1.0.18

CopyableTextInput​

CopyableTextInput renders an editable text input, an optional visible prefix, and the standard Chassis copy button. The button copies the configured prefix plus the input's current value. The prefix is rendered outside the HTML input, so Django receives and saves only the value entered into the field.

from django import forms

from django_chassis.widgets import CopyableTextInput


class PersonForm(forms.Form):
phone = forms.CharField(
widget=CopyableTextInput(
prefix='+375',
attrs={
'autocomplete': 'tel-national',
'inputmode': 'numeric'
}
)
)

The shared Chassis Admin base template already loads the copy client and component styles. Use the widget on a page rendered through Chassis Admin.

Date and time inputs​

DateInput uses DD.MM.YYYY, TimeInput uses HH:MM:SS, and DateTimeInput combines the two in one field. SplitDateTime uses separate date and time inputs. ChassisAdminMixin uses SplitDateTime by default for model datetime fields.

In a separate form, set the field's input_formats to the widget's exported DATE_INPUT_FORMAT, TIME_INPUT_FORMAT, or DATETIME_INPUT_FORMAT. Pair SplitDateTime with forms.SplitDateTimeField and pass input_date_formats and input_time_formats. Load the Admin JavaScript translation catalog and the form's media for the calendar and clock shortcuts. See the date and time recipe for a complete form.

Localized model fields and interface text​

Added in 1.0.23

TranslateWidget displays a textarea for each active language. TranslateField selects it automatically in a ModelForm. See the localization recipe for model fields and the translation catalog.

Additional form controls and source editors​

Added in 1.0.23
  • UnitInput('kg') works with Django's numeric form fields, with the unit displayed beside the input.
  • BooleanChoiceSwitch is a two-position widget for choices with the exact submitted values True and False.
  • TransferSelect uses Django Admin's two-list widget. Outside Admin pages, include the Admin JavaScript translation catalog with form.media.
  • PermissionGrid accepts a ModelMultipleChoiceField for Permission objects. Use select_related('content_type') on its queryset.

Use CodeEditor(language) from django_chassis.widgets to add highlighting to an existing text field. It accepts json, xml, css, javascript and html. To add syntax validation, use the corresponding form field.

Import JSONEditorField, XMLEditorField, CSSEditorField, JavaScriptEditorField and HTMLEditorField from django_chassis.forms. JSON returns its parsed value. XML, CSS, JavaScript and HTML return the source string. Install django-chassis[document-editors] to enable their syntax parsers. The editor displays source as text and validators only parse syntax. Render form.media to load the scoped stylesheet and highlighting script.

For example, declare a configuration form and use its cleaned_data only after is_valid(). Include {{ form.media }} in the template that renders it.

from django import forms

from django_chassis.forms import JSONEditorField
from django_chassis.widgets import BooleanChoiceSwitch, UnitInput


class CatalogOptionsForm(forms.Form):
max_weight = forms.DecimalField(widget=UnitInput(unit='kg'))
visible = forms.TypedChoiceField(
choices=[(True, 'Yes'), (False, 'No')],
coerce=lambda value: value == 'True',
widget=BooleanChoiceSwitch
)
rules = JSONEditorField(required=False)

JSONEditorField uses Django's JSON field and rejects nonstandard constants such as NaN. XML validation rejects DTDs and external entities. CSS, JavaScript and HTML use the optional syntax grammars and report invalid source with a line and column. If you render user-supplied source outside the editor, keep normal output escaping or apply your application's sanitization rules before interpreting it.