Skip to main content

A django-tables2 Column for status/tag fields with a Bootstrap popover header and colored badge cells. Cotton-friendly.

Project description

django-cotton-status

A django-tables2 Column that renders a status / tag field as a Bootstrap popover header (documenting each possible value) plus a colored badge in each cell — all from a single declaration.

Designed for projects using Django Cotton and Bootstrap 5.

What it gives you

from django_cotton_status import StatusColumn

class OrderTable(tables.Table):
    status = StatusColumn(
        label='Status',
        items=[
            {'value': 'PENDING',  'label': 'Pending',  'badge': 'warning', 'description': 'Awaiting confirmation.'},
            {'value': 'SHIPPED',  'label': 'Shipped',  'badge': 'info',    'description': 'En route to customer.'},
            {'value': 'DELIVERED','label': 'Delivered','badge': 'success', 'description': 'Customer received.'},
            {'value': 'CANCELED', 'label': 'Canceled', 'badge': 'danger',  'description': 'Order canceled.'},
        ],
    )

That single line gives you:

  • A column header with an info icon. Hovering or focusing the icon shows a popover listing every possible status with its colored badge and description.
  • Cells rendered as <span class="badge rounded-pill bg-{color}">{label}</span> based on the row's value.

No more duplicated ESTADO_BADGE = {...} dicts and render_status methods scattered across your codebase. One list of items defines everything.

Requirements

Installation

pip install django-cotton-status

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'django_cotton',
    'django_tables2',
    'django_cotton_status',
]

Bootstrap popover initialization

Bootstrap 5 does not auto-initialize popovers — you need to call new bootstrap.Popover(el) per trigger. If your project uses HTMX, you also need to clean up and re-init on swap, otherwise orphaned popovers stay floating and freshly-swapped triggers stay dead.

Drop this once in your base template:

<script>
    function cleanupPopovers() {
        document.querySelectorAll('.popover').forEach(el => el.remove());
    }

    function initPopovers() {
        cleanupPopovers();
        document.querySelectorAll('[data-bs-toggle="popover"]').forEach(el => {
            if (el._popover) {
                el._popover.dispose();
                el._popover = null;
            }
            el._popover = new bootstrap.Popover(el);
        });
    }

    document.addEventListener('DOMContentLoaded', initPopovers);
    document.body.addEventListener('htmx:beforeSwap', cleanupPopovers);
    document.body.addEventListener('htmx:afterSettle', initPopovers);
    window.addEventListener('beforeunload', cleanupPopovers);
</script>

htmx:afterSettle fires after HTMX swap animations finish, so new triggers are stable when initialized.

Usage

Main use case — StatusColumn

import django_tables2 as tables
from django_cotton_status import StatusColumn

ORDER_STATUSES = [
    {'value': 'PENDING',  'label': 'Pending',  'badge': 'warning', 'description': 'Awaiting confirmation.'},
    {'value': 'SHIPPED',  'label': 'Shipped',  'badge': 'info',    'description': 'En route to customer.'},
    {'value': 'DELIVERED','label': 'Delivered','badge': 'success', 'description': 'Customer received.'},
    {'value': 'CANCELED', 'label': 'Canceled', 'badge': 'danger',  'description': 'Order canceled.'},
]

class OrderTable(tables.Table):
    status = StatusColumn(label='Status', items=ORDER_STATUSES)

    class Meta:
        model = Order
        fields = ('status',)

If a row's value isn't in items, the cell falls back to the raw value (no badge).

Cotton component (outside tables)

For places that aren't a Column — detail page headers, cards, anywhere — use the cotton component:

<c-status-popover label="Status" :items="order_statuses" />

Same look, same data shape (without value, since there's nothing to match against).

Python helper (advanced)

If you build your own verbose_name and need just the popover string:

from django_cotton_status import info_popover

label_html = info_popover(
    'Status',
    items=[
        {'badge': 'success', 'label': 'Approved',  'description': '...'},
        {'badge': 'danger',  'label': 'Rejected',  'description': '...'},
    ],
)
# label_html is a Django SafeString — safe to inject into HTML attributes.

Item shape

{
    'value': 'APPROVED',                # required for StatusColumn cell rendering
    'label': 'Approved',                # visible chip text
    'badge': 'success',                 # Bootstrap color name
    'description': 'Approved order.',   # popover body text
}

For the cotton component and info_popover(), the value field is optional (only used by StatusColumn to match the cell value).

Bootstrap colors supported: primary, secondary, success, danger, warning, info, light, dark.

Customization

Placement

status = StatusColumn(label='Status', items=..., placement='right')

Accepts any value Bootstrap supports: top, bottom, left, right, auto.

Icon

By default the trigger uses fas fa-circle-info (Font Awesome). Override:

# Bootstrap Icons
status = StatusColumn(label='Status', items=..., icon_class='bi bi-info-circle')

Status

Alpha. API may change before 1.0.

License

MIT — see LICENSE.

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_cotton_status-0.1.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_cotton_status-0.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file django_cotton_status-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django_cotton_status-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2c2990e4c3499770d1570cd43183beb665581bf0e8fa495c47fb1fc3c88e5b1b
MD5 0abac8874c620c988142c22bc59a36ec
BLAKE2b-256 617a40009f219159d162e5b5be0cfaf001a48efc112b2be2fcfcd9a84e509882

See more details on using hashes here.

File details

Details for the file django_cotton_status-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_cotton_status-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a51ba8663b7be53bfb5305e9257e12ef9da3072c30ecde300c79fb47cfb51648
MD5 f2c6ebcdef2163c81a398e12ee830b18
BLAKE2b-256 aece51e9a6c359b39f0d4d23a24148ddddb845aa982f8f6438ef5747b243d86b

See more details on using hashes here.

Supported by

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