django-view-decorator is decorator aimed at bringing locality of behaviour to the connection between a URL and a view in Django.
Read more about the motivation behind the package in a recent blogpost.
Table of Contents
pip install django-view-decorator
Setup the project URLconf to include URLs from django-view-decorator:
# project/urls.py (this is what we point the ROOT_URLCONF setting at)
from django.urls import path
from django_view_decorator import include_view_urls
urlpatterns = [
path("", include_view_urls()),
]
Use the decorator like so, for:
# foos/views.py
from django_view_decorator import view
@view(paths="/foo/", name="foo")
def foo(request: HttpRequest) -> HttpResponse:
return HttpResponse("foo")
@view(paths="/foo/", name="foo-list")
class FooList(ListView):
model = Foo
paths
: is in a plural form because support also a list of pathsname
the classic route namenamespace
: {I don't know what to write here}extra_modules
: the python module itselfs (require a list)extra_packages
: can be a real path (require a list)login_required
: so you can avoid the Django decoratorpermission_required
: so you can avoid the Django decorator
import os
# We need the project directory
root = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
urlpatterns = [
path("", include_view_urls(extra_modules=[root + "/portal/views"])),
]
git clone
cd django-view-decorator
pip install hatch
hatch run tests:cov
hatch run tests:typecheck
django-view-decorator
is distributed under the terms of the MIT license.