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

Uploaded Source

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