A comprehensive demonstration of HTMX integration with Django, showcasing various interactive web techniques without writing JavaScript.
This project demonstrates how to use HTMX with Django to create modern, interactive web applications with minimal JavaScript. HTMX allows you to access AJAX, CSS Transitions, WebSockets, and Server Sent Events directly in HTML, using attributes, so you can build dynamic interfaces with the simplicity of Django's template system.
The showcase includes the following examples:
Load additional content when a button is clicked without refreshing the page.
Submit forms and update the DOM with server responses, all without page refreshes.
Automatically load more content when the user scrolls to the bottom of the page.
Filter and search through content as you type, with real-time results.
Sort table data by clicking on column headers, with backend processing.
Open modal dialogs with content loaded from the server.
- Django 5.0.6 - Python web framework
- HTMX 2.0.4 - Modern browser features, directly in HTML
- Bulma CSS - CSS framework for styling
- Docker - Containerization
- uv - Fast Python package installer and resolver
- Faker - Generating test data
- Docker and Docker Compose
- Git
-
Clone the repository:
git clone https://github.com/Krytos/Django-HTMX-Docker-Template.git cd Django-HTMX-Docker-Template
-
Start the Docker containers:
docker compose up app
-
The application will be available at http://localhost:8000
docker compose --profile test watch
django-htmx-showcase/
├── app/
│ ├── htmx_project/ # Django project settings
│ ├── showcase/ # Main application with HTMX examples
│ │ ├── migrations/ # Database migrations
│ │ ├── templates/ # HTML templates (examples organized here)
│ │ │ ├── base.html # Base template with common elements
│ │ │ ├── index.html # Home page with links to all examples
│ │ │ ├── partials/ # Partial templates for HTMX responses
│ │ ├── admin.py # Django admin configuration
│ │ ├── forms.py # Forms for the application
│ │ ├── models.py # Data models
│ │ ├── urls.py # URL routing
│ │ └── views.py # View functions for all examples
│ ├── manage.py # Django management script
│ ├── populate_contacts.py # Script to generate sample data
│ ├── Dockerfile # Docker configuration for the app
│ └── pyproject.toml # Python dependencies and project config
└── compose.yaml # Docker Compose configuration
HTMX allows you to make AJAX requests directly from HTML attributes. When combined with Django's template system, this enables dynamic web applications with minimal JavaScript:
- HTML elements use attributes like
hx-get
,hx-post
,hx-trigger
, etc. - HTMX intercepts these events and makes requests to your Django endpoints
- Django views return HTML fragments (usually from partial templates)
- HTMX updates the DOM with the returned HTML
A typical HTMX + Django pattern:
-
In your template:
<button hx-get="{% url 'load_more' %}" hx-target="#content" hx-swap="appendChild"> Load More </button> <div id="content"> <!-- Content goes here --> </div>
-
In your
urls.py
:path('load-more/', views.load_more, name='load_more')
-
In your
views.py
:def load_more(request): # Get some data return render(request, 'partials/more_content.html', {'data': data})
-
In your partial template (
partials/more_content.html
):<div class="item">{{ data }}</div>
This project is licensed under the MIT License - see the LICENSE file for details.