Skip to main content

A context decorator for Django views

Project description

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.


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'

    def context_variable(self):
        return 'context value'

    def context_property(self):
        return 'context 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.


Due to the usage of __set_name__, this package is limited to usage with Python 3.6+.


All code resides in 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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_context_decorator-1.5.0.tar.gz (2.8 kB view hashes)

Uploaded Source

Built Distribution

django_context_decorator-1.5.0-py3-none-any.whl (2.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page