Skip to main content

Bringing together django, django rest framework, and htmx

Project description

django-htmx-rest

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.1b1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

django_htmx_rest-0.0.1b1-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file django-htmx-rest-0.0.1b1.tar.gz.

File metadata

  • Download URL: django-htmx-rest-0.0.1b1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for django-htmx-rest-0.0.1b1.tar.gz
Algorithm Hash digest
SHA256 f91427f0690a029bf61754d43832d90718511f554b8ec2448e852a4fd4f6c519
MD5 6ee676da325344f46dac98f59a93aab9
BLAKE2b-256 ec85c6f2f3be5b1efd1788b6bb122bbebaf226ef4155296a263fc0e5eb35e6b3

See more details on using hashes here.

Provenance

File details

Details for the file django_htmx_rest-0.0.1b1-py3-none-any.whl.

File metadata

  • Download URL: django_htmx_rest-0.0.1b1-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for django_htmx_rest-0.0.1b1-py3-none-any.whl
Algorithm Hash digest
SHA256 50ac530a6d8b40ee2f0b6ee4033e1522963abfde3b9583e1fe5c3843dcc5d8dd
MD5 e0d890126173607e34b7cd5899cbdcca
BLAKE2b-256 eb5b92e2bcee5bc573b5bbea86dd2cc3cb3777ae1f12b859e5f4d1e725ec51c1

See more details on using hashes here.

Provenance

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