Mixins
- class terminusgps.django.mixins.HtmxTemplateResponseMixin[source]
Renders a partial HTML template depending on HTTP headers.
Public Methods:
render_to_response
(context, **response_kwargs)Sets
template_name
topartial_template_name
if necessary.Inherited from
TemplateResponseMixin
render_to_response
(context, **response_kwargs)Return a response, using the response_class for this view, with a template rendered with the given context.
Return a list of template names to be used for the request.
Usage
# views.py
from django.views.generic import TemplateView
from terminusgps.django.mixins import HtmxTemplateResponseMixin
# The mixin **MUST** be inherited before a Django view, otherwise the view's MRO will break.
class HtmxTemplateView(HtmxTemplateResponseMixin, TemplateView):
content_type = "text/html"
template_name = "htmx.html"
partial_template_name = "partials/_htmx.html" # Rendered if htmx request is made.
Common practice is to include the partial template in the main template:
<!-- templates/htmx.html -->
{% extends "layout.html" %}
{% block content %}
{% include "partials/_htmx.html" %}
{% endblock content %}