Skip to content

Commit

Permalink
Merge pull request #1 from Erteno/asgi
Browse files Browse the repository at this point in the history
Initial work for supporting ASGI. https://github.com/adamghill/django:
Merged ASGI Branch until PR adamghill#121 is resolved
  • Loading branch information
Thembahank authored Apr 8, 2021
2 parents c836bf1 + 24d7e0c commit 423c972
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
9 changes: 9 additions & 0 deletions django_unicorn/templates/unicorn/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
<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, "{{ CSRF_HEADER_NAME }}");

</script>
{% else %}
<script type="module">
Expand All @@ -14,7 +19,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, "{{ CSRF_HEADER_NAME }}");
</script>
{% endif %}
6 changes: 6 additions & 0 deletions django_unicorn/templatetags/unicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def unicorn_scripts():

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)
),
"CSRF_HEADER_NAME": csrf_header_name,
}

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"),
)
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 @@ -53,6 +53,9 @@
]

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


CACHES = {
Expand Down

0 comments on commit 423c972

Please sign in to comment.