Skip to main content

Bringing together django, django rest framework, and htmx

Project description

django-htmx-rest

tox

A library for bringing together the Django REST Framework and htmx.

htmx is a new javascript library based on Intercooler.js that allows you to access modern browser features directly from HTML, rather than using javascript.

This library is super tiny for now, and focuses on providing a framework for backend patterns that support htmx, like content negotiation and improvements to DRF's browsable API.

Features

Content Negotiation

We extend the default content negotiation behavior of DRF to serve a partial template in the presence of htmx's Hx-Request header. Otherwise, the htmx partial is wrapped in a full page. This does, however override DRF's browsable API.

In the future, the plan is to enhance DRF's browsable API, and to serve the htmx component alongside the browsable data for a request for text/html without the Hx-Request header. This would allow the same view to serve JSON, the htmx partial content, and a full browsable page for developers. This feature can also include basic frontend development tooling like live reloading and viewport resizing, like what Storybook and other isolated UI development tools provide, allowing rapid development of htmx partials.

Future Features

New Spin on the Browsable API

With htmx, your browsable API is not just a view into your application data, but also a sort of storybook for UI components, as expressed by HTMX partials.

If you directly visit a htmx-rest view in your browser, you will still be able to access the DRF browsable API, but you'll also be able to see and interact with the HTMX component in isolation with live reloading, and basic UI development tools, like changing the viewport size, and viewing the context data being passed into the template

Also, to help you browse through all your htmx-rest views, our browsable interface has a sidebar listing all of the htmx-rest views in your application, so you can explore them.

Setup

First, install the package:

pip install django-htmx-rest

Then, add put the following in your settings.py file:

# settings.py

INSTALLED_APPS = [
    ...
    'htmx_rest'
    ...
]


REST_FRAMEWORK = {

    'DEFAULT_CONTENT_NEGOTIATION_CLASS':
        'htmx_rest.negotiator.HtmxContentNegotiator',

    'DEFAULT_RENDERER_CLASSES': [
        'rest_framework.renderers.JSONRenderer',

         # optional, if you want the browsable api
        'rest_framework.renderers.BrowsableAPIRenderer',

        # this or some renderer other than the BrowsableAPIRenderer that can
        # render content_types of `text/html` must be present
        'htmx_rest.renderers.HTMXPartialTemplateRenderer',
    ]
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-htmx-rest-0.0.1b2.tar.gz (6.9 kB view hashes)

Uploaded Source

Built Distribution

django_htmx_rest-0.0.1b2-py3-none-any.whl (16.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page