{"id":19253865,"url":"https://github.com/rixx/django-context-decorator","last_synced_at":"2025-04-21T14:32:18.221Z","repository":{"id":57419518,"uuid":"181154789","full_name":"rixx/django-context-decorator","owner":"rixx","description":"Django @context decorator","archived":false,"fork":false,"pushed_at":"2024-03-07T13:58:42.000Z","size":18,"stargazers_count":22,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T13:51:16.446Z","etag":null,"topics":["django"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rixx.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-13T10:27:46.000Z","updated_at":"2024-10-08T14:51:21.000Z","dependencies_parsed_at":"2024-06-19T17:52:55.732Z","dependency_job_id":null,"html_url":"https://github.com/rixx/django-context-decorator","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rixx%2Fdjango-context-decorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rixx%2Fdjango-context-decorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rixx%2Fdjango-context-decorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rixx%2Fdjango-context-decorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rixx","download_url":"https://codeload.github.com/rixx/django-context-decorator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249972875,"owners_count":21354079,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django"],"created_at":"2024-11-09T18:33:45.439Z","updated_at":"2025-04-21T14:32:17.956Z","avatar_url":"https://github.com/rixx.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"django-context-decorator\n------------------------\n\n.. image:: https://img.shields.io/travis/rixx/django-context-decorator.svg\n   :target: https://travis-ci.org/rixx/django-context-decorator\n   :alt: Continuous integration\n\n.. image:: https://img.shields.io/codecov/c/github/rixx/django-context-decorator.svg\n   :target: https://codecov.io/gh/rixx/django-context-decorator\n   :alt: Coverage\n\n.. image:: https://img.shields.io/pypi/v/django-context-decorator.svg\n   :target: https://pypi.python.org/pypi/django-context-decorator\n   :alt: PyPI\n\n``django-context-decorator`` is a Python package for Django removing the need\nto call ``super().get_context_data(**kwargs)`` in nearly every Django view.\n\nYou can also read the `blog post`_ about this package.\n\n***Note:*** If this package is looking unmaintained due to no recent commits, that’s\nbecause it’s a tiny package that is feature complete and stable. I see no need to\nrelease a new version just to add explicit compatibility with new Django versions,\nbut for as long as Django provides class-based views with a ``get_context_data``\nmethod, this package will work with them.\n\nUsage\n=====\n\n.. code-block:: python\n\n   from django_context_decorator import context\n   from django.utils.functional import cached_property\n   from django.views.generic import TemplateView\n\n   class MyView(TemplateView):\n       template_name = 'path/to/template.html'\n\n       @context\n       def context_variable(self):\n           return 'context value'\n\n       @context\n       @property\n       def context_property(self):\n           return 'context property'\n\n       @context\n       @cached_property\n       def expensive_context_property(self):\n           return 'expensive context property'\n\nNow you'll have access to ``{{ context_variable }}``, ``{{ context_property }}``\nand ``{{ expensive_context_property }}`` in your template.\n\nPlease note: While this package works with the ``@cached_property`` decorator,\nplease make sure to add the ``@context`` decorator **above** the\n``@cached_property`` decorator.\n\nThis is especially useful when you couple it with inheritance, because it\nallows you to re-use parent class variables without having to extract them from\nyour ``context``. So you could write a long-form like this:\n\n.. code-block:: python\n\n    from django.views.generic import TemplateView\n\n    \n    class BaseMixin:\n\n        def get_context_data(self, **kwargs):\n            ctx = super().get_context_data(**kwargs)\n            ctx['var_from_base_mixin'] = 'var_from_base_mixin'\n            return ctx\n\n\n    class View1(BaseMixin, TemplateView):\n\n        def get_context_data(self, **kwargs):\n            ctx = super().get_context_data(**kwargs)\n            ctx['var_from_view_1'] = 'value_from_view_1'\n            return ctx\n\n\n    class View2(View1):\n\n        def get_context_data(self, **kwargs):\n            ctx = super().get_context_data(**kwargs)\n            ctx['var_from_view_2'] = 'value_from_view_2'\n            return ctx\n\ninstead like this:\n\n.. code-block:: python\n\n    from django.views.generic import TemplateView\n    from django_context_decorator import context\n\n\n    class BaseMixin:\n\n        @context\n        def var_from_base_mixin(self):\n            return 'var_from_base_mixin'\n\n\n    class View1(BaseMixin, TemplateView):\n\n        @context\n        def var_from_view_1(self):\n            return 'value_from_view_1'\n\n\n    class View2(View1):\n\n        @context\n        def var_from_view_2(self):\n            return 'value_from_view_2'\n\n\nDevelopment\n===========\n\nAll code resides in ``django_context_decorator.py``. Tests are collected by\n``pytest`` from all files starting with ``test_``. To run tests, start a\nvirtual environment, install the dependencies, and run ``pytest``::\n\n    pip install django pytest pytest-cov\n    pytest --cov-report term --cov=django_context_decorator\n\n.. _blog post: https://rixx.de/blog/a-context-decorator-for-django/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frixx%2Fdjango-context-decorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frixx%2Fdjango-context-decorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frixx%2Fdjango-context-decorator/lists"}