Skip to main content

No project description provided

Project description

Django View Utils

Tests Documentation PyPI - Version PyPI - Python Version


django-view-utils aims to be a collection of useful utilities for Django views. Mainly function based views.

Features

@view decorator

The @view decorator is a simple way to create a view function and register it with a URL.

# <app>/views.url

from django_view_utils import view


@view(paths="/hello-world/", name="hello-world")
def my_view(request):
    ...


# <app>/urls.py

from django.urls import path
from django_view_utils import include_view_urls

urlpatterns = [
    path("", include_view_urls()),
]

By default, django-view-utils will look for a views.py file in your app.

This can be disabled by setting DJANGO_VIEW_UTILS_AUTO_DISCOVER to False, then registering view modules is up to you by supplying the modules keyword argument to include_view_urls.

@view decorator options

Conveniently it also supports login_required, staff_required and permission_required.

@view(paths="/hello-world/", name="hello-world", login_required=True)
def my_view(request):
    ...

@view(paths="/hello-world/", name="hello-world", staff_required=True)
def my_view(request):
    ...

@view(paths="/hello-world/", name="hello-world", permissions=["myapp.can_do_something"])
def my_view(request):
    ...

Inspiration

This decorator is very much inspired by the idea of "locality of behaviour" by Carson Gross (creator of HTMX): https://htmx.org/essays/locality-of-behaviour/.

It also bears resemblance to the @app.route decorator in Flask, the @app.<HTTP method> decorator in FastAPI and probably many other Python web frameworks.

Table of Contents

Installation

pip install django-view-utils

Development

git clone
cd django-view-utils
pip install hatch
hatch run tests:cov
hatch run tests:typecheck

License

django-view-utils is distributed under the terms of the MIT license.

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_view_utils-0.0.4.tar.gz (4.9 kB view hashes)

Uploaded Source

Built Distribution

django_view_utils-0.0.4-py3-none-any.whl (6.5 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