Skip to content

# Mixins

class terminusgps.django.mixins.HtmxTemplateResponseMixin

  Renders a partial HTML template depending on HTTP headers.

  htmx documentation

  Public Data Attributes:

  `partial_template_name` | A partial template rendered by htmx.

  Inherited from `TemplateResponseMixin`

  `template_name` |
  `template_engine` |
  `content_type` |

  Public Methods:

  `render_to_response`(context, **response_kwargs) | Sets `template_name` to `partial_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.
  `get_template_names`() | Return a list of template names to be used for the request.

  partial_template_name: str | None = None

    A partial template rendered by htmx.

    Type:

      `str` | `None`

    Value:

      `None`

  render_to_response(context: dict[str, Any], **response_kwargs: Any) → HttpResponse

    Sets `template_name` to `partial_template_name` if necessary.

## 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 %}