liveBook · Manning

Revealed

As explained in Django in Action

revealed is an HTMX trigger value that fires when an element becomes visible in the browser viewport.

`revealed` is an HTMX trigger value for lazy loading: it fires an HTMX request the first time its element enters the browser viewport. Place it on a small sentinel element after a page of results, and scrolling can fetch the next page without a Next button. section 12.4

For infinite search results, render the current page plus a sentinel only while more results exist. The sentinel’s `hx-get` includes the next page and current search text; `hx-swap="outerHTML"` replaces the loading element with the newly returned results and, when needed, the next sentinel. section 12.4

Loading the next musician results page

`<div hx-get="...?page={{next_page}}&search_text={{search_text}}" hx-trigger="revealed" hx-swap="outerHTML"><i>Loading...</i></div>` appears only when `has_more` is true. Although a `div` is normally invisible, HTMX can still detect when it enters the viewport. A visible loading message makes slower requests less mysterious. section 12.4

Listing 12.8 The inner template for the search results
<!-- RiffMates/templates/partials/musician_results.html -->
<ul>
  {% for musician in musicians %}  #1
    <li> <a href="{% url 'musician' musician.id %}">
      {{musician.last_name}}, {{musician.first_name}}
    </a> </li>
  {% empty %}
    <li> <i>No matching musicians</i> </li>
  {% endfor %}
</ul>

Browse the results partial that renders the current musician list before the pagination sentinel is added. section 12.4

How Django in Action covers it

How deep it goes
Explains the viewport trigger through a paginated Django view and template request flow.
Examples
Demonstrates musician search results with a Loading message, delayed responses, and 80 generated musicians for testing.
What you can do after
Apply it via HTMX to request and replace successive pages of search results while users scroll.
How it connects
Connects HTMX to infinite scrolling, pagination, `hx-get`, `hx-swap`, and Django partial rendering.

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.