Skip to content
Draft
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
8 changes: 8 additions & 0 deletions django_unicorn/templates/unicorn/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
<script src="{% static 'js/unicorn.min.js' %}"></script>

<script>
{% if IS_ASGI %}
var url = "{% url 'django_unicorn:message_async' %}";
{% else %}
var url = "{% url 'django_unicorn:message' %}";
{% endif %}
Unicorn.init(url);
</script>
{% else %}
Expand All @@ -14,7 +18,11 @@
// Set Unicorn to the global so it can be used by components
window.Unicorn = Unicorn;

{% if IS_ASGI %}
var url = "{% url 'django_unicorn:message_async' %}";
{% else %}
var url = "{% url 'django_unicorn:message' %}";
{% endif %}
Unicorn.init(url);
</script>
{% endif %}
10 changes: 9 additions & 1 deletion django_unicorn/templatetags/unicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@

@register.inclusion_tag("unicorn/scripts.html")
def unicorn_scripts():
return {"MINIFIED": get_setting("MINIFIED", not settings.DEBUG)}
return {
"MINIFIED": get_setting("MINIFIED", not settings.DEBUG),
"IS_ASGI": hasattr(settings, "ASGI_APPLICATION")
and bool(settings.ASGI_APPLICATION)
and (
not hasattr(settings, "WSGI_APPLICATION")
or not bool(settings.WSGI_APPLICATION)
),
}


@register.inclusion_tag("unicorn/errors.html", takes_context=True)
Expand Down
11 changes: 8 additions & 3 deletions django_unicorn/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@

urlpatterns = (
re_path("message/(?P<component_name>[\w/\.-]+)", views.message, name="message"),
path(
"message", views.message, name="message"
), # Only here to build the correct url in scripts.html
re_path(
"message-async/(?P<component_name>[\w/\.-]+)",
views.message_async,
name="message_async",
),
# Only here to build the correct url in scripts.html
path("message", views.message, name="message"),
path("message-async", views.message_async, name="message_async"),
)
6 changes: 6 additions & 0 deletions django_unicorn/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,9 @@ def message(request: HttpRequest, component_name: str = None) -> JsonResponse:
)

return JsonResponse(res)


async def message_async(
request: HttpRequest, component_name: str = None
) -> JsonResponse:
return message(request, component_name)
8 changes: 8 additions & 0 deletions example/project/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os

from django.core.asgi import get_asgi_application


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")

application = get_asgi_application()
3 changes: 3 additions & 0 deletions example/project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
]

WSGI_APPLICATION = "project.wsgi.application"
ASGI_APPLICATION = (
"project.asgi.application" # need to comment out WSGI_APPLICATION to enable ASGI
)


DATABASES = {
Expand Down