conjunto.htmx

conjunto.htmx.HxActionButton

Bases: HxButton

A renderable action button from an IActionButton instance.

Parameters:
  • request (HttpRequest) –

    the current request

  • action_button (IActionButton) –

    the IActionButton class

conjunto.htmx.HxButton

Render a HTMX enabled button.

Attributes:
  • url

    the text to display

  • icon

    the (optional) icon to display

  • dialog

    whether to open a modal dialog (sets hx-target to #dialog)

  • target

    the target to open the link in. Cannot be used together with dialog.

  • css_class

    the css classes to apply to the button

  • method

    the method to call on click ("get" or "post"). Default is "get".

  • view_name

    the name of the view to call on click, if no url is provided. if provided, the render method must be called with url params if they are needed to create the url using reverse(view_name, kwargs)

Example
{% hx_button url="person:add" method="post" icon="plus" dialog=True css_class="btn"

Render a hyperlink using HTMX.

Attributes:
  • url

    the URL.

  • text

    the text to display.

  • icon

    the (optional) icon to display before the text.

  • dialog

    whether to open a modal dialog. Defaults to False.

conjunto.htmx.IHtmxComponentMixin

Bases: HtmxResponseMixin

An interface Mixin that describes a component and can be rendered as plugin.

  1. Declare an interface for HTMX components
  2. Declare Implementations of that interface which also inherit from View.

Examples:

# in your plugin's api/interfaces.py, create an interface for your view
@Interface
class IUserProfileSection(IHtmxComponentMixin, ...):
    params = ["pk"]
    template_name = "common/layout/profile_section.html"
    ...

# in your plugin's views.py
def PasswordView(IUserProfileSection, UpdateView)
    ...
def OtherComponentView(IUserProfileSection, TemplateView)
    ...

Use them in your template:

{% for plugin in components.IUserProfileSection %}
    <a href="#"
       hx-get="{% url component.get_url_name user.id %}"
       hx-trigger="{% if component.selected %}load, {% endif %}click once"
       {# hx-push-url="{{ component.name }}" #}
       hx-target="#profile-content"
       class="list-group-item list-group-item-action d-flex align-items-center
        active">
        <i class="bi bi-{{ component.icon }} me-2"></i>
        {{ component.title }}
     </a>
{% endfor %}

You can also inherit from other mixins, e.g. PermissionRequiredMixin, etc.

class UserprofilePasswordSection(
    IUserProfileSection, PermissionRequiredMixin, TemplateView
):
    name = "password"
    permission_required = "..."
    template_name = "my_app/password_view.html

form_kwargs_request = False class-attribute instance-attribute

Should the evtl. attached form receive a request?

group = '' class-attribute instance-attribute

The group this component lives under. Can be used to display group headers.

icon: str = '' class-attribute instance-attribute

the icon name, if the component is listed in a list, and icons are used.

name: str = '' class-attribute instance-attribute

The view name of the component. Must be unique. Used in and URLs. Can be used for HTML attributes.

params: list[str] = [] class-attribute instance-attribute

A list of params the view is using. These must then be passed when calling the view from a template, e.g. via hx-get.

title: str = '' class-attribute instance-attribute

The title this plugin is rendered with. It's up to you how the plugin uses that title.

weight: int = 0 class-attribute instance-attribute

The weight this component is ranked like menu items. The more weight, the more the component sinks "down" in the list.

enabled()

Hook for implementations to define if the component is enabled.

Returns:
  • bool

    True if the component is enabled, False if not.

get_name()

Returns the component's view (machine) name.

get_title()

returns the component's title.

Use it in templates like {{ component.get_title }}

get_url_name()

Returns the component's url.'

Use it as {% url component.get_url ... %} in a template.

get_url_patterns() classmethod

Convenience method to add to your urls.py in an include section:

Example
url_patterns = [
    path(...),
    path("profile/",
        include(
            (IUserProfileSectionView.get_url_patterns(), "profile"),
            namespace="profile"
        )
    ),
]

# or directly add the url_patterns to the main list:

url_patterns += IUserProfileSectionView.get_url_patterns()

get_urlpattern()

Calculates the urlpattern where this component is accessible.

That can be used e.g. in hx-get attributes. Returns: a URLPattern that can be used in your urls.py

conjunto.htmx.Icon

Render an icon using Bootstrap 5.

Attributes:
  • name

    the name of the icon

conjunto.htmx.UseComponentMixin

A mixin that can be added to a View that uses HTMX components.

This can be used if e.g. a View has tabs, or variable parts of the page that are extended by a component. The URL is pushed automatically with the new component as configurable GET parameter, e.g.: example.com/persons/42?tab=account

Example
class MyView(UseComponentMixin, DetailView):
    components = [PersonAccountTab]
Attributes:
  • components (list[IHtmxComponentMixin]) –

    a list of Interfaces that are used in the template of this view, and can be accessed there using components.IFooInterface.

query_variable = 'tab' class-attribute instance-attribute

The query variable that is used to indicate the active component.