A context decorator for Django views
Project description
django-context-decorator is a Python package for Django removing the need to call super().get_context_data(**kwargs) in nearly every Django view.
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.
Limitations
Due to the usage of __set_name__, this package is limited to usage with Python 3.6+.
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 py.test --cov-report term --cov=django_context_decorator
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_context_decorator-1.5.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 389f67e54079c3b1dd130ced31ade9bf79045f456d39b050c940d61135dccb9a |
|
MD5 | 9700f8668c8a8dfa62916d469cd7bcb2 |
|
BLAKE2b-256 | 32758bf5c0492791baf69da873b0af2e3fbf7fb66a1ca110cfb0f910cc184ee8 |
Hashes for django_context_decorator-1.5.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe71ce1f822999c84e29b01fdd87de75fcda8979057bc0a2af916bed4e818106 |
|
MD5 | 7625c2e8293d4eb013f232f5fb7eb60f |
|
BLAKE2b-256 | a6f3cf0a3644996f07f970508ba08cbf8cfaf01d0b52873e042df2dbac75b030 |