Skip to main content

HTMX renderer for Django REST FRAMEWORK

Project description

DRF HTMX Renderer

DRF HTMX Renderer is a (renderer)[https://www.django-rest-framework.org/api-guide/renderers/] for Django REST Framework.

In the vain of the browsable API or the admin renderer shipped with DRF, it allows for browsing the API directly in your browser and making it fully human-friendly.

To learn more about this project, you can watch this talk - Slides available here.

The aim of the project is to provide a customizable and extendable interface for data management that can be presented to the final user.

Install

With pip

pip install drf-htmx-renderer

From github

git clone git@github.com:nanuxbe/drf_htmx_renderer.git
cd drf_htmx_renderer
pip install -e .
pip install -r requirements-dev.txt

python manage.py runserver

Settings

Add these to Django settings.py

DRF_AUTO_METADATA_ADAPTER = 'htmx_renderer.adapters.HTMXEndpointAdapter'
DRF_AUTO_BASE_SERIALIZER = 'htmx_renderer.serializers.HTMXModelSerializer'
DRF_AUTO_BASE_VIEWSET = 'htmx_renderer.views.ModelViewSet'

REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': [
        'htmx_renderer.renderers.TemplateHTMLRenderer',
        'rest_framework.renderers.JSONRenderer',
    ],
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 50,
}

Add htmx_renderer in to INSTALLED_APPS:

INSTALLED_APPS = [
    ...

    # API
    'rest_framework',
    'drf_auto_endpoint',
    'htmx_renderer',

    # TEMPLATES
    'bootstrap5',
    'mathfilters',
]

URLs

Add this to the main urls.py

from drf_auto_endpoint.router import router

urlpatterns = [
    ...
    path('api/v1/', include(router.urls)),
    ...
]

Getting started

Create an endpoint for your model

Inside <your_app>/endpoints.py create an endpoint:

from drf_auto_endpoint.endpoints import Endpoint
from drf_auto_endpoint.router import register

from .models import MyModel


@register
class MyModelEndpoint(Endpoint):
    model = MyModel

Restart Django's development serve and point it to http://localhost:8000/api/v1/

For further customization of your endpoint, refer to DRF-Schema-Adapter's documentation

Building

This project uses standardized pyproject.toml for the package. To build it simply invoke

python -m build

(note, you may need to install build package first).

To install package in development mode you can use

pip install -e .

Development

Sample (demo) app

One way to start the development is to run our demo app and explore examples. Source code is available in sample subdirectory.

Create migrations

python manage.py migrate

Start development server

python manage.py runserver

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

drf-htmx-renderer-0.0.2.tar.gz (6.7 kB view details)

Uploaded Source

File details

Details for the file drf-htmx-renderer-0.0.2.tar.gz.

File metadata

  • Download URL: drf-htmx-renderer-0.0.2.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for drf-htmx-renderer-0.0.2.tar.gz
Algorithm Hash digest
SHA256 6b3c89b7b5109dfea15df0ab8596149e5aa187a9f1b534038005e00baebc459d
MD5 bdb6fd523e577c1f1cc87a9570344311
BLAKE2b-256 bf9a961e5906a21b15a4bf9d0265b4d10a07713a1765a940e18f6e49db0ff095

See more details on using hashes here.

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