Skip to content
Django @context decorator
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Add gitignore Apr 13, 2019
.travis.yml Add travis config Apr 13, 2019
README.rst added an example that illustrates the benefits of using the decorator Apr 19, 2019
django_context_decorator.py
setup.py Fix PyPI URL Apr 14, 2019
test_context.py Fix context bleed in inheritance trees Apr 14, 2019
test_context_renaming.py suppress > except: pass Apr 14, 2019

README.rst

django-context-decorator

Continuous integration Coverage PyPI

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.

Another example to illustrate the benefits of the use of the decorator.

Instead of the code:

from django.views.generic import TemplateView


class BaseMixin(TemplateView):

    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):

    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

you could implement it as:

from django.views.generic import TemplateView
from django_context_decorator import context


class BaseMixin(TemplateView):

    @context
    def var_from_base_mixin(self):
        return 'var_from_base_mixin'


class View1(BaseMixin):

    @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 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
pytest --cov-report term --cov=django_context_decorator
You can’t perform that action at this time.