django-context-decorator
django-context-decorator is a Python package for Django removing the need to call super().get_context_data(**kwargs) in nearly every Django view.
You can also read the blog post about this package.
*Note:* If this package is looking unmaintained due to no recent commits, that’s because it’s a tiny package that is feature complete and stable. I see no need to release a new version just to add explicit compatibility with new Django versions, but for as long as Django provides class-based views with a get_context_data method, this package will work with them.
Usage
from django_context_decorator import context
from django.utils.functional import cached_property
from django.views.generic import TemplateView
class MyView(TemplateView):
template_name = 'path/to/template.html'
@context
def context_variable(self):
return 'context value'
@context
@property
def context_property(self):
return 'context property'
@context
@cached_property
def expensive_context_property(self):
return 'expensive context property'
Now you’ll have access to {{ context_variable }}, {{ context_property }} and {{ expensive_context_property }} in your template.
Please note: While this package works with the @cached_property decorator, please make sure to add the @context decorator above the @cached_property decorator.
This is especially useful when you couple it with inheritance, because it allows you to re-use parent class variables without having to extract them from your context. So you could write a long-form like this:
from django.views.generic import TemplateView
class BaseMixin:
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['var_from_base_mixin'] = 'var_from_base_mixin'
return ctx
class View1(BaseMixin, TemplateView):
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['var_from_view_1'] = 'value_from_view_1'
return ctx
class View2(View1):
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['var_from_view_2'] = 'value_from_view_2'
return ctx
instead like this:
from django.views.generic import TemplateView
from django_context_decorator import context
class BaseMixin:
@context
def var_from_base_mixin(self):
return 'var_from_base_mixin'
class View1(BaseMixin, TemplateView):
@context
def var_from_view_1(self):
return 'value_from_view_1'
class View2(View1):
@context
def var_from_view_2(self):
return 'value_from_view_2'
Development
All code resides in django_context_decorator.py. Tests are collected by pytest from all files starting with test_. To run tests, start a virtual environment, install the dependencies, and run pytest:
pip install django pytest pytest-cov pytest --cov-report term --cov=django_context_decorator
Metadata
Release files for django-context-decorator 1.6.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django_context_decorator-1.6.1.tar.gz | 2.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_context_decorator-1.6.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 6.1 kB
Release files / django_context_decorator-1.6.1.tar.gz
| Download URL | django_context_decorator-1.6.1.tar.gz |
|---|---|
| Size | 2.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8907905dec5d7b1c565074f57a5d1db1b3c4454d9dedb334bd4848f2ca6b54ec
|
|
BLAKE2b-256 checksum How to use checksums |
d52efa70983c8377d55168788ec3019f66034c511e360bccb50556a0ec26351d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|
Release files / django_context_decorator-1.6.1-py3-none-any.whl
| Download URL | django_context_decorator-1.6.1-py3-none-any.whl |
|---|---|
| Size | 3.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
66be0f51b61cd1d859f779fd49de62e7227a626677fed02d0de75a8362e0fce1
|
|
BLAKE2b-256 checksum How to use checksums |
f9af76c3190700dd00ef7e5d5fca9c0431c8d29c2d317388604d03b30d933cc1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|
Security for django-context-decorator 1.6.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.