Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for polling with reswap and HttpResponseStopPolling #412

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions example/example/templates/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ <h1>
<li>
<a href="/error-demo/">Error Demo</a>
</li>
<li>
<a href="/polling-demo/">Polling Demo</a>
</li>
<li>
<a href="/middleware-tester/">Middleware Tester</a>
</li>
Expand Down
15 changes: 15 additions & 0 deletions example/example/templates/polling-demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "_base.html" %}

{% block main %}
<section>
<p>
This example shows how to use <a href="https://dev.htmx.org/attributes/hx-trigger/">the <code>hx-trigger</code> attribute</a> to poll a server for updates until it responds with <a href="https://dev.htmx.org/docs/#polling">HTTP 286</a>.
</p>
</section>
<section>
<blockquote hx-get="/polling-demo/" hx-trigger="every 1s" hx-target="find span">
"<span>...</span>"
<footer><i>- Bono, U2 - Vertigo, 2004</i></footer>
</blockquote>
</section>
{% endblock %}
1 change: 1 addition & 0 deletions example/example/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
path("csrf-demo/checker/", views.csrf_demo_checker),
path("error-demo/", views.error_demo),
path("error-demo/trigger/", views.error_demo_trigger),
path("polling-demo/", views.polling_demo),
path("middleware-tester/", views.middleware_tester),
path("middleware-tester/table/", views.middleware_tester_table),
path("partial-rendering/", views.partial_rendering),
Expand Down
23 changes: 23 additions & 0 deletions example/example/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from django.views.decorators.http import require_POST
from faker import Faker

from django_htmx.http import HttpResponseStopPolling
from django_htmx.http import reswap
from django_htmx.middleware import HtmxDetails
from example.forms import OddNumberForm

Expand Down Expand Up @@ -76,6 +78,27 @@ def error_demo_trigger(request: HtmxHttpRequest) -> HttpResponse:
return render(request, "error-demo.html") # unreachable


# Polling Demo

_countdown = None


@require_GET
def polling_demo(request: HtmxHttpRequest) -> HttpResponse:
global _countdown

if request.htmx:
try:
return HttpResponse(next(_countdown))
except StopIteration:
response = HttpResponseStopPolling()
reswap(response, "none")
return response

_countdown = iter(["Unos", "Dos", "Tres", "Catorce!"])
return render(request, "polling-demo.html")


# Middleware tester

# This uses two views - one to render the form, and the second to render the
Expand Down