Skip to main content

A handy collection of reusable Django UI components, allowing me to share and integrate elements efficiently across various projects.

Project description

django-sample-components

PyPI

A reusable Django library that provides ready-to-use UI components — template tags, templates, and static assets — for use across Django projects.

Components are split into two groups:

  • Static ({% load sample_tags %}) — pure server-side rendered components, no JavaScript dependencies beyond Bootstrap.
  • Async ({% load async_tags %}) — interactive components powered by HTMX that update without page reloads.

Installation

pip install django-sample-components
# or
poetry add django-sample-components

Add to INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    # ...
    'django_sample_components',
    'django_htmx',   # required for async components
]

Add HtmxMiddleware to MIDDLEWARE (required for async components):

MIDDLEWARE = [
    # ...
    'django_htmx.middleware.HtmxMiddleware',
]

Include the library URLs in your project's urls.py:

from django.urls import include, path

urlpatterns = [
    path('components/', include('django_sample_components.urls')),
]

Collect static files:

python manage.py collectstatic

Static Components

Load with {% load sample_tags %} in any template. No HTMX or JavaScript required (except Bootstrap JS for simple_popup).

Tag Type Quick usage
greeting simple_tag {% greeting "Alice" %}
show_today_timestamp simple_tag {% show_today_timestamp %}
simple_typewriter simple_tag {% simple_typewriter words %}
simple_button simple_tag {% simple_button "Label" href="/url" btn_type="primary" %}
shout block_tag {% shout %}...{% endshout %}
simple_alert block_tag {% simple_alert type="success" %}...{% endsimple_alert %}
simple_popup block_tag {% simple_popup name_button="Open" title="Title" %}...{% endsimple_popup %}

Quick example

{% load sample_tags %}

{% simple_alert type="success" %}
    Your changes have been saved.
{% endsimple_alert %}

{% simple_popup name_button="Open" title="Confirm" size="sm" %}
    <p>Are you sure?</p>
{% endsimple_popup %}

{% simple_button "Download" href="/files/report.pdf" icon_before="fa fa-download" %}

Async Components (HTMX)

Load with {% load async_tags %}. These components require HTMX and django-htmx. The easiest way to set them up is to extend master_async.html, which includes HTMX and handles CSRF automatically.

Tag Quick usage Doc
async_counter {% async_counter initial_value=0 step=1 %} docs
async_active_search {% async_active_search search_url="/search/" %} docs
async_lazy_popup {% async_lazy_popup name_button="Open" content_url="/content/" %} docs

Quick example

{% load async_tags %}

{# Counter with step 5, bounded 0–100 #}
{% async_counter initial_value=0 step=5 min_value=0 max_value=100 %}

{# Live search against your own endpoint #}
{% async_active_search search_url="/contacts/search/" placeholder="Search contacts..." %}

{# Modal that loads content only when opened #}
{% async_lazy_popup name_button="View Report" title="Monthly Report"
                    content_url="/reports/monthly/" size="lg" %}

HTMX setup in your own base template

If you are not extending master_async.html, add the following to your base template:

{% load django_htmx %}

<head>
    {% htmx_script %}
    {% django_htmx_script %}
</head>
<body hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'>
    ...
</body>

Static vs Async — key differences

Static (sample_tags) Async (async_tags)
Rendered Server-side, with the page On demand via HTMX
JavaScript None (Bootstrap JS for popups) HTMX required
Page reload N/A Never
Dependencies Bootstrap CSS/JS Bootstrap CSS/JS + HTMX + django-htmx
Best for Simple UI elements Interactive, live, or heavy-content components

Running locally

git clone https://github.com/GustavoRizzo/django-sample-components.git
cd django-sample-components
poetry install
poetry run task run-demo

Open http://127.0.0.1:8000 to browse the component showcase.

Tests

poetry run task test

Publishing

poetry version patch   # bump version (e.g. 0.1.0 → 0.1.1)
poetry build
poetry publish

Update the version in both pyproject.toml and django_sample_components/__init__.py before releasing.

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_sample_components-0.1.7.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

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

django_sample_components-0.1.7-py3-none-any.whl (41.4 kB view details)

Uploaded Python 3

File details

Details for the file django_sample_components-0.1.7.tar.gz.

File metadata

  • Download URL: django_sample_components-0.1.7.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.10.12 Linux/6.6.87.2-microsoft-standard-WSL2

File hashes

Hashes for django_sample_components-0.1.7.tar.gz
Algorithm Hash digest
SHA256 cf354133de63efef9f68468afe3530dd05d0b283a3518e903c0a76d2f3d3b365
MD5 ac3a0ef48474cb7134b655b3d7b50634
BLAKE2b-256 011a798826ef6ff05e33dba6f0e47f4028a24eed394eff6bbd936aa6f06cbf00

See more details on using hashes here.

File details

Details for the file django_sample_components-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: django_sample_components-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 41.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.10.12 Linux/6.6.87.2-microsoft-standard-WSL2

File hashes

Hashes for django_sample_components-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 bd4d0401ddb27f0e68a5b84b4bdafd6224618e38360f100a6c53df64dd4dccd4
MD5 c9d85c86d0c8c5db72da77801a8eaf09
BLAKE2b-256 3578511a96b2e3185d36d4aad460241cdaece39b3395d238ac893891724d9226

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