mirror of
https://github.com/django-components/django-components.git
synced 2025-11-19 14:26:46 +00:00
| .. | ||
| images | ||
| component.py | ||
| page.py | ||
| README.md | ||
| test_example_fragments.py | ||
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:
- Make a server request
- Server responds with new HTML
- 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.
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"),
]
