crispy-tailwind Documentation

repository·main·Indexed 19 days ago

https://github.com/django-crispy-forms/crispy-tailwind

A Tailwind CSS template pack for django-crispy-forms that allows developers to render Django forms with Tailwind CSS styling. It includes layout descriptors such as Submit, Reset, Button, and Alert, as well as a CSSContainer class for managing and composing Tailwind CSS classes for form elements and widgets.

Tokens
2K
Snippets
9
Records
10
Agent score
64%

What's inside crispy-tailwind

  1. Style custom widgets in crispy-tailwind

    main

    The template pack provides default styles for standard Django widgets. To apply custom Tailwind classes to specific widget instances, use the widget.attrs argument when defining the field in your Django Form.

    class CustomTextWidget(forms.TextInput):
        pass
    
    class CustomTextWidgetForm(forms.Form):
        name = forms.CharField(
            widget=CustomTextWidget(attrs={"class": "custom-css"})
        )
  2. Use the {% crispy %} tag with FormHelper and Layout

    main

    To use django-crispy-forms features like FormHelper and Layout with Tailwind, follow these steps:

    1. Load the crispy_forms_tags in your template.
    2. Configure a FormHelper instance within your Django Form class.
    3. Use the {% crispy %} tag in your template, passing the form instance as an argument.
    {% load crispy_forms_tags %}
    {% crispy form %}
  3. Install crispy-tailwind

    main

    Install the package via pip and configure your Django settings to enable the Tailwind template pack.

    1. Install the package: pip install crispy-tailwind

    2. Update INSTALLED_APPS in your settings.py to include both crispy_forms and crispy_tailwind.

    3. Set CRISPY_ALLOWED_TEMPLATE_PACKS and CRISPY_TEMPLATE_PACK to 'tailwind'.

    INSTALLED_APPS = (
        ...
        "crispy_forms",
        "crispy_tailwind",
        ...
    )
    
    CRISPY_ALLOWED_TEMPLATE_PACKS = "tailwind"
    
    CRISPY_TEMPLATE_PACK = "tailwind"
  4. How get_input_class resolves widget names

    main

    The get_input_class method maps a Django field to a CSS class attribute on the CSSContainer instance. It works by taking the class name of the field's widget, converting it to lowercase, and stripping the suffixes widget or input.

    Example Mapping Logic:

    • TextInput $\rightarrow$ text
    • EmailInput $\rightarrow$ email
    • CheckboxInput $\rightarrow$ checkbox
    • SelectMultiple $\rightarrow$ selectmultiple (Note: the regex removes widget or input from the end)

    If a mapping for the resulting name does not exist in the CSSContainer, it returns an empty string "".

  5. Use CSSContainer to manage Tailwind CSS classes

    main

    The CSSContainer class is used to manage and compose Tailwind CSS classes for various form elements (widgets) and error states. It allows you to define a base set of classes that apply to all elements, and then specialize classes for specific widget types or add/remove classes using set operations.

    Key Features:

    • Base Classes: Setting a base key in the input dictionary applies those classes to all supported widget types.
    • Specialization: Providing specific keys (e.g., text, checkbox, error_border) overrides or extends the base classes for those specific elements.
    • Class Composition: Use the + operator to add classes to specific fields and the - operator to remove them.
    • Automatic Widget Mapping: The get_input_class method automatically resolves a Django field to its corresponding CSS class string based on its widget type.
    from crispy_tailwind.tailwind import CSSContainer
    
    # Initialize with base classes and specific overrides
    styles = {
        "base": "block w-full rounded-md border-gray-300 shadow-sm",
        "text": "focus:border-indigo-500 focus:ring-indigo-500",
        "checkbox": "h-4 w-4 rounded border-gray-300 text-indigo-600",
        "error_border": "border-red-500"
    }
    container = CSSContainer(styles)
    
    # Add more classes to specific widgets using +
    container + {"text": "extra-padding", "checkbox": "mt-1"}
    
    # Remove classes using -
    container - {"text": "extra-padding"}
    
    # Get the class for a specific Django field
    # (Assumes 'my_field' is a Django field with a TextInput widget)
    class_name = container.get_input_class(my_field)
    print(class_name)  # Output will include base and text classes
  6. Use Alert components

    main

    The Alert class is a Tailwind-compatible version of the standard crispy_forms.bootstrap.Alert. It is used to display alert messages within a form layout. It currently defaults to an empty css_class string, allowing you to pass specific Tailwind alert classes during instantiation.

    from crispy_tailwind.layout import Alert
    
    # Example usage
    alert = Alert('Success!', 'success-alert-class')
  7. Create a generic Button with Button

    main

    The Button class is used to create a generic button descriptor (type button) for use within a Layout object passed to the {% crispy %} template tag. By default, it applies Tailwind CSS classes for a blue button: bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded.

    Arguments:

    • The first argument is the label text. This text is also slugified and used as the id for the button.
    • css_class: An optional string to override the default Tailwind styling.
    from crispy_tailwind.layout import Button
    
    # Example usage in a Layout
    btn = Button('Click Me', 'click-me')
  8. Create a Submit button with Submit

    main

    The Submit class is used to create a submit button descriptor for use within a Layout object passed to the {% crispy %} template tag. By default, it applies Tailwind CSS classes for a green button: bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded.

    Arguments:

    • The first argument is the label text. This text is also slugified and used as the id for the button.
    • css_class: An optional string to override the default Tailwind styling.
    from crispy_tailwind.layout import Submit
    
    # Example usage in a Layout
    submit = Submit('Search the Site', 'search this site')
    
    # Example with custom styling
    custom_submit = Submit('Save', 'save', css_class='bg-black text-white')
  9. Create a Reset button with Reset

    main

    The Reset class is used to create a reset button descriptor for use within a Layout object passed to the {% crispy %} template tag. By default, it applies Tailwind CSS classes for a red button: bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded.

    Arguments:

    • The first argument is the label text. This text is also slugified and used as the id for the button.
    • css_class: An optional string to override the default Tailwind styling.
    from crispy_tailwind.layout import Reset
    
    # Example usage in a Layout
    reset = Reset('Reset This Form', 'Revert Me!')