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.
You can also read the blog post about this package.
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'
Limitations
Due to the usage of __set_name__, this package is limited to 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 pytest --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.6.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a70acd422165807710873cfa5e0cbf9e24ad2548b065f726e404f9473997e29 |
|
MD5 | 5a338b1c0ab020f4b5cf62b8ba9f239f |
|
BLAKE2b-256 | e9b6438d311b3b53e92ecc8083b890758157120dad763567bbb3e42b439a3947 |
Hashes for django_context_decorator-1.6.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b4ba53811362c359221da243dd8db19c02891bb493e984e4dbe5edfd54f40b5 |
|
MD5 | fdc67689201bac9c902318d2af053aef |
|
BLAKE2b-256 | 659d4f4ab5c8d7d4355fe57d68d4f114d7dfa37c19facfea54c3328caa04bd3b |