-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extension script with error handler (#88)
Fixes #86.
- Loading branch information
1 parent
f5bfc0b
commit 3873500
Showing
17 changed files
with
232 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{% extends "_base.html" %} | ||
|
||
{% block main %} | ||
<section> | ||
<p> | ||
This page shows you django-htmx’s error handler. | ||
</p> | ||
</section> | ||
<section> | ||
<p> | ||
{% if DEBUG %} | ||
The error handler will work, since <code>DEBUG = True</code>. | ||
{% else %} | ||
The error handler will <strong>not</strong> work, since <strong>DEBUG = False</strong>. | ||
{% endif %} | ||
</p> | ||
</section> | ||
<section> | ||
<button hx-get="/error-demo/trigger/"> | ||
Try divide by zero | ||
</button> | ||
</section> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from django.conf import settings | ||
from django.templatetags.static import static | ||
from django.utils.html import format_html | ||
|
||
|
||
def django_htmx_script(): | ||
# Optimization: whilst the script has no behaviour outside of debug mode, | ||
# don't include it. | ||
if not settings.DEBUG: | ||
return format_html("") | ||
return format_html( | ||
'<script type="text/javascript" src="{}" data-debug="{}" async defer></script>', | ||
static("django-htmx.js"), | ||
str(bool(settings.DEBUG)), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
const data = document.currentScript.dataset; | ||
const isDebug = data.debug === "True"; | ||
|
||
if (isDebug) { | ||
document.addEventListener("htmx:beforeOnLoad", function (event) { | ||
const xhr = event.detail.xhr; | ||
if (xhr.status == 500) { | ||
// Tell htmx to stop processing this response | ||
event.stopPropagation(); | ||
|
||
document.children[0].innerHTML = xhr.response; | ||
|
||
// Run Django’s inline script | ||
// (1, eval) wtf - see https://stackoverflow.com/questions/9107240/1-evalthis-vs-evalthis-in-javascript | ||
(1, eval)(document.scripts[0].innerText); | ||
// Need to directly call Django’s onload function since browser won’t | ||
window.onload(); | ||
} | ||
}); | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django import template | ||
|
||
from django_htmx.jinja import django_htmx_script | ||
|
||
register = template.Library() | ||
register.simple_tag(django_htmx_script) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
SECRET_KEY = "NOTASECRET" | ||
|
||
DATABASES = {} | ||
|
||
ALLOWED_HOSTS = [] | ||
|
||
INSTALLED_APPS = [] | ||
DATABASES = {} | ||
|
||
INSTALLED_APPS = [ | ||
"django_htmx", | ||
] | ||
|
||
MIDDLEWARE = [] | ||
|
||
TEMPLATES = [ | ||
{ | ||
"BACKEND": "django.template.backends.django.DjangoTemplates", | ||
"DIRS": [], | ||
"OPTIONS": {"context_processors": []}, | ||
} | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from django.template import Context, Template | ||
from django.test import SimpleTestCase, override_settings | ||
|
||
|
||
class DjangoHtmxScriptTests(SimpleTestCase): | ||
def test_non_debug_empty(self): | ||
result = Template("{% load django_htmx %}{% django_htmx_script %}").render( | ||
Context() | ||
) | ||
|
||
assert result == "" | ||
|
||
def test_debug_success(self): | ||
with override_settings(DEBUG=True): | ||
result = Template("{% load django_htmx %}{% django_htmx_script %}").render( | ||
Context() | ||
) | ||
|
||
assert result == ( | ||
'<script type="text/javascript" src="django-htmx.js" ' | ||
+ 'data-debug="True" async defer></script>' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from django.test import SimpleTestCase, override_settings | ||
|
||
from django_htmx.jinja import django_htmx_script | ||
|
||
|
||
class DjangoHtmxScriptTests(SimpleTestCase): | ||
def test_non_debug_empty(self): | ||
result = django_htmx_script() | ||
|
||
assert result == "" | ||
|
||
def test_debug_success(self): | ||
with override_settings(DEBUG=True): | ||
result = django_htmx_script() | ||
|
||
assert result == ( | ||
'<script type="text/javascript" src="django-htmx.js" ' | ||
+ 'data-debug="True" async defer></script>' | ||
) |