lively-traffic-17360
01/06/2022, 6:01 PMlively-traffic-17360
01/06/2022, 6:02 PMlively-traffic-17360
01/06/2022, 6:03 PMplain-kangaroo-26043
01/06/2022, 6:03 PMlively-traffic-17360
01/06/2022, 6:03 PMlively-traffic-17360
01/06/2022, 6:07 PMfresh-controller-36545
01/06/2022, 6:13 PMplain-kangaroo-26043
01/06/2022, 6:14 PMfresh-controller-36545
01/06/2022, 6:14 PMHX-Trigger-After-Settle
? Honestly unsure why it doesn't work.plain-kangaroo-26043
01/06/2022, 6:16 PMfresh-controller-36545
01/06/2022, 6:17 PMfragments
/ partials
(really gotta norm that term)fresh-controller-36545
01/06/2022, 6:18 PMform partial
lively-traffic-17360
01/06/2022, 6:18 PMlively-traffic-17360
01/06/2022, 6:20 PMlively-traffic-17360
01/06/2022, 7:34 PMwhite-motorcycle-95262
01/07/2022, 12:31 AMhundreds-camera-24900
01/07/2022, 12:34 AMfresh-controller-36545
01/07/2022, 12:34 AMpython
class HtmxTemplateResponseMixin(TemplateResponseMixin):
htmx_template_name: str = None
def get_template_names(self):
"""
Return a list of template names to be used for the request. Must return
a list. May not be called if render_to_response() is overridden.
"""
super().get_template_names()
if self.template_name is None or self.htmx_template_name is None:
raise ImproperlyConfigured(
"HtmxTemplateResponseMixin requires either a definition of "
"'htmx_template_name' and 'template_name' or an "
"implementation of 'get_template_names()'"
)
if self.request.htmx and not self.request.htmx.boosted:
return [self.htmx_template_name]
return [self.template_name]
white-motorcycle-95262
01/07/2022, 12:36 AMtemplate_names
dict, where default
key goes to the full template, and then keys for each id go to template snippets for only updating portions of the page.fresh-controller-36545
01/07/2022, 12:36 AMwhite-motorcycle-95262
01/07/2022, 12:40 AMclass HtmxFormView(HtmxTemplateResponseMixin, FormView):
"""Extend Django's FormView for HTMX."""
def form_invalid(self, form) -> HttpResponse:
"""Returns form template with errors."""
if 'form' not in self.template_names:
message = (
"HtmxFormView requires a `form` value for `template_names`."
)
raise ImproperlyConfigured(message)
response = render(
self.request,
template_name=self.template_names['form'],
context=self.get_context_data(),
)
response['HX-Retarget'] = self.request.htmx.trigger
return response
But in my situation all the forms are fetched by HTMX and only one form is displayed at a time.mysterious-toddler-20573
01/07/2022, 12:40 AMwhite-motorcycle-95262
01/07/2022, 12:40 AMform_valid
would be need set on each view that subclasses HtmxFormView
, since that's a bit specificwhite-motorcycle-95262
01/07/2022, 12:45 AMHtmxTemplateResponseMixin
class HtmxTemplateResponseMixin(TemplateResponseMixin):
"""Extend Django's Class Based Viws for HTMX."""
template_names = None
def get_template_names(self) -> list:
"""Fetches proper template depending on request."""
if self.template_names is None:
message = (
"HtmxTemplateResponseMixin requires either a definiton of "
"the `template_names` dictionary or an implementation of "
"`get_template_names()`."
)
raise ImproperlyConfigured(message)
try:
template_name = self.template_names['default']
except KeyError:
message = (
"HtmxTemplateResponseMixing requires that the `template_names` "
"dictionary have a value for `default` or an implementation of "
"`get_template_names()`."
)
raise ImproperlyConfigured(message)
if self.request.htmx:
try:
template_name = self.template_names[self.request.htmx.target]
except KeyError:
pass
return [template_name]
This can be used to get the following CBV:
class HtmxTemplateView(HtmxTemplateResponseMixin, TemplateView):
"""Extend Django's TemplateView for HTMX."""
pass
class HtmxDetailView(HtmxTemplateResponseMixin, DetailView):
"""Extend Django's TemplateView for HTMX."""
pass
class HtmxListView(HtmxTemplateResponseMixin, ListView):
"""Extend Django's ListView for HTMX."""
pass
fresh-controller-36545
01/07/2022, 12:46 AMforms
to edit with a single CBV when using some sort of form mixin. I know somewhat unrelated, but…fresh-controller-36545
01/07/2022, 12:47 AMdjango-htmx
– though the community is somewhat emotionally split between explicit
and implicit
views/apis.fresh-controller-36545
01/07/2022, 12:50 AMhtmx_template_name
– quite like it actually.white-motorcycle-95262
01/07/2022, 12:52 AM{% if not request.htmx %}
<head>[head stuff, meta, etc, load css]</head>
<body>
<nav> [ navbar stuff that's on every page] </nav>
<main id="content">
{% endif %}
[ content ]
{% if not request.htmx %}
</main>
<footer> [footer info that's on every page] </footer>
{% block footer_js %}[load js libs]{% endblock %}
{% endif %}
fresh-controller-36545
01/07/2022, 12:55 AMwhite-motorcycle-95262
01/07/2022, 12:57 AMhx-boost
there's no need to reload them, so yeah.
(although, I suppose browsers cache css and js libs and such, but I'm not 100% on how that works).