Skip to content

Commit 47bdabe

Browse files
committed
Extend example app to have an async version.
1 parent c03f08f commit 47bdabe

File tree

10 files changed

+63
-0
lines changed

10 files changed

+63
-0
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ example:
66
--noinput --username="$(USER)" --email="$(USER)@mailinator.com"
77
python example/manage.py runserver
88

9+
example_async:
10+
python example/manage.py migrate --noinput
11+
-DJANGO_SUPERUSER_PASSWORD=p python example/manage.py createsuperuser \
12+
--noinput --username="$(USER)" --email="$(USER)@mailinator.com"
13+
daphne example.asgi:application
14+
15+
916
test:
1017
DJANGO_SETTINGS_MODULE=tests.settings \
1118
python -m django test $${TEST_ARGS:-tests}

docs/changes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Serializable (don't include in main)
2323
* Make ``Panel.panel_id`` a class member.
2424
* Update all panels to utilize data from ``Panel.get_stats()`` to load content
2525
to render. Specifically for ``Panel.title`` and ``Panel.nav_title``.
26+
* Extend example app to contain an async version.
2627

2728
Pending
2829
-------

example/asgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for example_async project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example.async_.settings")
15+
16+
application = get_asgi_application()

example/async_/__init__.py

Whitespace-only changes.

example/async_/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Django settings for example project."""
2+
3+
from ..settings import * # noqa: F403
4+
5+
ROOT_URLCONF = "example.async_.urls"

example/async_/urls.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.urls import path
2+
3+
from example.async_ import views
4+
from example.urls import urlpatterns as sync_urlpatterns
5+
6+
urlpatterns = [
7+
path("async/db/", views.async_db_view, name="async_db_view"),
8+
*sync_urlpatterns,
9+
]

example/async_/views.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.contrib.auth.models import User
2+
from django.http import JsonResponse
3+
4+
5+
async def async_db_view(request):
6+
names = []
7+
async for user in User.objects.all():
8+
names.append(user.username)
9+
return JsonResponse({"names": names})

example/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
MIDDLEWARE = [
2929
"debug_toolbar.middleware.DebugToolbarMiddleware",
3030
"django.middleware.security.SecurityMiddleware",
31+
"whitenoise.middleware.WhiteNoiseMiddleware",
3132
"django.contrib.sessions.middleware.SessionMiddleware",
3233
"django.middleware.common.CommonMiddleware",
3334
"django.middleware.csrf.CsrfViewMiddleware",

example/templates/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ <h1>Index of Tests</h1>
2323
<button id="incrementFetch" data-url="{% url 'ajax_increment' %}" type="button">Increment via fetch</button>
2424
<button id="incrementXHR" data-url="{% url 'ajax_increment' %}" type="button">Increment via XHR</button>
2525
</p>
26+
<button id="asyncRequest" data-url="{% url 'async_db_view' %}" type="button">Make async call</button>
27+
2628
<script>
2729
const incrementFetch = document.querySelector("#incrementFetch");
2830
const incrementXHR = document.querySelector("#incrementXHR");
31+
const asyncButton = document.querySelector("#asyncRequest");
2932
const value = document.querySelector("#session-value");
3033
incrementFetch.addEventListener("click", function () {
3134
fetch(incrementFetch.dataset.url).then( function (response) {
@@ -44,6 +47,14 @@ <h1>Index of Tests</h1>
4447
xhr.open('GET', incrementXHR.dataset.url, true);
4548
xhr.send('');
4649
});
50+
asyncButton.addEventListener("click", function () {
51+
fetch(asyncButton.dataset.url).then( function (response) {
52+
response.json().then(function(data) {
53+
console.log(data)
54+
});
55+
});
56+
});
57+
4758
</script>
4859
</body>
4960
</html>

requirements_dev.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Django
44
sqlparse
55
Jinja2
66

7+
# Django Async
8+
daphne
9+
whitenoise # To avoid dealing with static files
10+
711
# Testing
812

913
coverage[toml]

0 commit comments

Comments
 (0)