From 823735113059b48dcaf6c439d71cbe7a5088afc9 Mon Sep 17 00:00:00 2001 From: George Katsikas <giorgakis.katsikas@gmail.com> Date: Wed, 14 Jun 2023 12:01:23 +0200 Subject: [PATCH] display django messages on HTMX events --- .../static/scipost/assets/css/_messages.scss | 3 +-- .../scipost/templates/scipost/_hx_messages.html | 10 ++++++++++ .../scipost/templates/scipost/bare_base.html | 1 + .../scipost/templates/scipost/messages.html | 16 ++++++---------- scipost_django/scipost/urls.py | 1 + scipost_django/scipost/views.py | 4 ++++ 6 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 scipost_django/scipost/templates/scipost/_hx_messages.html diff --git a/scipost_django/scipost/static/scipost/assets/css/_messages.scss b/scipost_django/scipost/static/scipost/assets/css/_messages.scss index 163fff905..3061f3da4 100644 --- a/scipost_django/scipost/static/scipost/assets/css/_messages.scss +++ b/scipost_django/scipost/static/scipost/assets/css/_messages.scss @@ -3,8 +3,7 @@ padding-right: 10px; position: fixed; bottom: 0px; - left: 0px; - width: 100%; + right: 0px; z-index: 9999; } diff --git a/scipost_django/scipost/templates/scipost/_hx_messages.html b/scipost_django/scipost/templates/scipost/_hx_messages.html new file mode 100644 index 000000000..c19a73bb7 --- /dev/null +++ b/scipost_django/scipost/templates/scipost/_hx_messages.html @@ -0,0 +1,10 @@ +{% for message in messages %} + <div class="alert alert-{{ message.tags }} alert-dismissible" role="alert"> + <button type="button" + class="btn-close" + style="top: unset !important" + data-bs-dismiss="alert" + aria-label="Close"></button> + {{ message|safe|escape }} + </div> +{% endfor %} diff --git a/scipost_django/scipost/templates/scipost/bare_base.html b/scipost_django/scipost/templates/scipost/bare_base.html index 1840faaf4..ae45a6e61 100644 --- a/scipost_django/scipost/templates/scipost/bare_base.html +++ b/scipost_django/scipost/templates/scipost/bare_base.html @@ -34,6 +34,7 @@ {% block breadcrumb %}{% endblock breadcrumb %} {% block secondary_navbar %}{% endblock secondary_navbar %} + {% include 'scipost/messages.html' %} diff --git a/scipost_django/scipost/templates/scipost/messages.html b/scipost_django/scipost/templates/scipost/messages.html index c48de0b27..47813aaf7 100644 --- a/scipost_django/scipost/templates/scipost/messages.html +++ b/scipost_django/scipost/templates/scipost/messages.html @@ -1,10 +1,6 @@ -<div class="alert-fixed-container"> - {% for message in messages %} - <div class="alert alert-{{ message.tags }} alert-dismissible" role="alert"> - <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"> - <span aria-hidden="true">×</span> - </button> - {{ message|safe|escape }} - </div> - {% endfor %} -</div> +<div id="global-message-container" + class="alert-fixed-container" + hx-get="{% url 'scipost:_hx_messages' %}" + hx-trigger="load, htmx:trigger from:body delay:1000" + hx-swap="beforeend" + hx-sync="body:drop"></div> diff --git a/scipost_django/scipost/urls.py b/scipost_django/scipost/urls.py index d82f43b8f..dabfb75db 100644 --- a/scipost_django/scipost/urls.py +++ b/scipost_django/scipost/urls.py @@ -156,6 +156,7 @@ urlpatterns = [ TemplateView.as_view(template_name="scipost/acknowledgement.html"), name="acknowledgement", ), + path("messages", views._hx_messages, name="_hx_messages"), # ####### # Info diff --git a/scipost_django/scipost/views.py b/scipost_django/scipost/views.py index 26ac38be5..bf8454174 100644 --- a/scipost_django/scipost/views.py +++ b/scipost_django/scipost/views.py @@ -175,6 +175,10 @@ def trigger_error(request): division_by_zero = 1 / 0 +def _hx_messages(request): + return render(request, "scipost/_hx_messages.html") + + ############# # Main view ############# -- GitLab