django-components/docs/examples/fragments
Juro Oravec 49afdb49d6
Some checks are pending
Docs - build & deploy / docs (push) Waiting to run
Run tests / build (ubuntu-latest, 3.10) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.11) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.12) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.13) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.8) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.9) (push) Waiting to run
Run tests / build (windows-latest, 3.10) (push) Waiting to run
Run tests / build (windows-latest, 3.11) (push) Waiting to run
Run tests / build (windows-latest, 3.12) (push) Waiting to run
Run tests / build (windows-latest, 3.13) (push) Waiting to run
Run tests / build (windows-latest, 3.8) (push) Waiting to run
Run tests / build (windows-latest, 3.9) (push) Waiting to run
Run tests / test_docs (3.13) (push) Waiting to run
Run tests / test_sampleproject (3.13) (push) Waiting to run
docs: Add "scenarios" code examples (#1445)
2025-10-08 00:17:31 +02:00
..
images docs: Add "scenarios" code examples (#1445) 2025-10-08 00:17:31 +02:00
component.py docs: Add "scenarios" code examples (#1445) 2025-10-08 00:17:31 +02:00
page.py docs: Add "scenarios" code examples (#1445) 2025-10-08 00:17:31 +02:00
README.md docs: Add "scenarios" code examples (#1445) 2025-10-08 00:17:31 +02:00
test_example_fragments.py docs: Add "scenarios" code examples (#1445) 2025-10-08 00:17:31 +02:00

HTML fragments

Fragments are pieces of HTML that are inserted into the page without a full page reload. Fragments are also known as partials or HTML-over-the-wire.

The usual flow is to:

  1. Make a server request
  2. Server responds with new HTML
  3. Insert the new HTML into the page

This example loads HTML fragments using different client-side techniques: vanilla JavaScript, AlpineJS, and HTMX.

In each of the 3 cases, when the fragment is loaded, this also runs the fragment's JS and CSS code.

Fragments example

Definition

--8<-- "docs/examples/fragments/component.py"

Example

To see the component in action, you can set up a view and a URL pattern as shown below.

views.py

--8<-- "docs/examples/fragments/page.py"

urls.py

from django.urls import path

from examples.pages.fragments import FragmentsPage

urlpatterns = [
    path("examples/fragments", FragmentsPage.as_view(), name="fragments"),
]