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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: django-htmx-rest-0.0.1b2.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.1b2.tar.gz
Algorithm Hash digest
SHA256 f0af19c91dd5e0f14f6c37c57e6bfbb6ff66fadb4a45eaa6bfe7cee9ec17a065
MD5 e1028e9aef8d782e5f4364f2f6ad3631
BLAKE2b-256 35a80ab119b56c95cfa0a89368f83d3bcb96bd870681913178e288e116266e4b

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: django_htmx_rest-0.0.1b2-py3-none-any.whl
  • Upload date:
  • Size: 16.0 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.1b2-py3-none-any.whl
Algorithm Hash digest
SHA256 a16ecc2d073d7260a64e967c756da05792a6a49383dbcd5196c752cf9afeac68
MD5 347fb457a6c1714603600ba72c93b6bc
BLAKE2b-256 c15ad921a78fa8503d04ed90d2e1b0adb37ec1af6b6dc8e320f35d0e582c6e56

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