{"id":14972833,"url":"https://github.com/federicobond/django-openfeature","last_synced_at":"2025-10-26T20:32:16.389Z","repository":{"id":228517721,"uuid":"773673081","full_name":"federicobond/django-openfeature","owner":"federicobond","description":"OpenFeature utilities for Django","archived":false,"fork":false,"pushed_at":"2024-06-10T21:05:31.000Z","size":43,"stargazers_count":2,"open_issues_count":3,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-01T00:15:03.808Z","etag":null,"topics":["django","django-framework","feature-flags","openfeature"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/federicobond.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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":"2024-03-18T07:32:27.000Z","updated_at":"2024-11-16T16:27:32.000Z","dependencies_parsed_at":"2024-06-10T23:11:26.337Z","dependency_job_id":null,"html_url":"https://github.com/federicobond/django-openfeature","commit_stats":{"total_commits":33,"total_committers":1,"mean_commits":33.0,"dds":0.0,"last_synced_commit":"70a1c5901c443bbb974151e26b4a155da33863b6"},"previous_names":["federicobond/django-openfeature"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/federicobond%2Fdjango-openfeature","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/federicobond%2Fdjango-openfeature/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/federicobond%2Fdjango-openfeature/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/federicobond%2Fdjango-openfeature/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/federicobond","download_url":"https://codeload.github.com/federicobond/django-openfeature/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238397215,"owners_count":19465135,"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","django-framework","feature-flags","openfeature"],"created_at":"2024-09-24T13:47:36.626Z","updated_at":"2025-10-26T20:32:11.084Z","avatar_url":"https://github.com/federicobond.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"django-openfeature\n==================\n\n![build status](https://github.com/federicobond/django-openfeature/actions/workflows/python-package.yml/badge.svg?branch=main)\n![release](https://img.shields.io/pypi/v/django-openfeature)\n![python versions](https://img.shields.io/pypi/pyversions/django-openfeature)\n\n\u003e ⚠️ Caution: this repository is a work-in-progress. ⚠️\n\nDjango OpenFeature is a set of utilities to use OpenFeature in your Django applications.\n\n## Features\n\n * Django Debug Toolbar integration\n * Templatetags for flag evaluation\n * Automatic evaluation context from request\n * Flag override mechanism for testing\n\n## Installation\n\n```\npip install django-openfeature\n```\n\nAdd `django_openfeature` to your `INSTALLED_APPS` setting.\n\n```python\nINSTALLED_APPS = [\n    # ...\n    'django_openfeature',\n    # ...\n]\n```\n\n## Usage\n\n### Flag Evaluation Helpers\n\ndjango-openfeature provides a set of helpers to evaluate a feature flag:\n\n```python\nfrom django_openfeature import feature\n\nfeature(request, 'my_feature', False)\n```\n\nThe `feature` function will infer the type of the feature flag based on the default value provided\nand call the appropriate resolver method.\n\nIt will also create an evaluation context from the request. This context can be configured via the\n`OPENFEATURE` setting (described below).\n\nInside templates, you can use the `feature` template tag to evaluate feature flags. Remember to load the\n`openfeature` library first.\n\n```html\n{% load openfeature %}\n\n{% feature 'my_feature' False as my_feature_enabled %}\n{% if my_feature_enabed %}\n    \u003cp\u003eFeature is enabled\u003c/p\u003e\n{% else %}\n    \u003cp\u003eFeature is disabled\u003c/p\u003e\n{% endif %}\n```\n\nAs an alternative to evaluating feature flags to a variable you can use the `iffeature` template tag.\nIt will output the contents of the block if the boolean flag resolves to True. It supports a single\n`{% else %}` clause that will be displayed otherwise.\n\n```html\n{% load openfeature %}\n\n{% iffeature 'my_feature' %}\n    \u003cp\u003eFeature is enabled\u003c/p\u003e\n{% else %}\n    \u003cp\u003eFeature is disabled\u003c/p\u003e\n{% endif %}\n```\n\n### Configuration\n\n```python\nOPENFEATURE = {\n    \"CONTEXT_EVALUATOR\": \"myapp.utils.get_evaluation_context\",\n}\n```\n\nThe `CONTEXT_EVALUATOR` setting should point to a function that receives a request and returns an OpenFeature EvaluationContext as defined in the `openfeature-sdk` package.\n\nAlternatively, the context evaluator can point to a class that produces callable instances with the same signature. The default context evaluator is `django_openfeature.context.OpenFeatureContext` and looks roughly like this:\n\n```python\nclass OpenFeatureContext:\n    targeting_key = \"id\"\n    targeting_key_anonymous = \"unknown\"\n    user_attributes = (\"username\", \"email\")\n\n    def get_context(self, request):\n        user = request.user\n        if not user or user.is_anonymous:\n            return EvaluationContext(self.targeting_key_anonymous)\n        return self.get_user_context(user)\n\n    def get_user_context(self, user):\n        attributes = {}\n        for attr in self.user_attributes:\n            attributes[attr] = getattr(user, attr)\n        targeting_key = str(getattr(user, self.targeting_key))\n        return EvaluationContext(targeting_key, attributes)\n\n    def __call__(self, request):\n        return self.get_context(request)\n```\n\nUse this class as base to declaratively define your own context evaluator. For example, to change the targeting key to the\nuser's username:\n\n```python\nfrom django_openfeature.context import OpenFeatureContext\n\nclass MyOpenFeatureContext(OpenFeatureContext):\n    targeting_key = \"username\"\n```\n\nThen in your settings:\n\n```python\nOPENFEATURE = {\n    \"CONTEXT_EVALUATOR\": \"myapp.utils.MyOpenFeatureContext\",\n}\n```\n\n### Debug Toolbar Panel\n\ndjango-openfeature comes with a Feature Flags panel for the Django Debug Toolbar. You can activate it by adding `openfeature.debug_toolbar.panels.FeatureFlagsPanel` to your `DEBUG_TOOLBAR_PANELS` setting.\n\n```python\nDEBUG_TOOLBAR_PANELS = [\n    # ...\n    'openfeature.debug_toolbar.panels.FeatureFlagsPanel',\n    # ...\n]\n```\n\nThe Feature Flags panel will show you the feature flag evaluations for the current request, the request evaluation context\nand the configured providers for those evaluations.\n\n\u003cimg width=\"928\" alt=\"Feature Flags Debug Toolbar Panel\" src=\"https://github.com/federicobond/django-openfeature/assets/138426/b22d5e1c-ac93-4a1f-af8d-e0206abc6c02\"\u003e\n\n\n### Testing Utilities\n\ndjango-openfeature provides a set of utilities to help you test your feature flags. To use them, you must set the provider to an instance of `django_openfeature.provider.DjangoTestProvider` in your test settings.\n\n```python\n# in your test settings\nimport openfeature.api\nfrom django_openfeature.provider import DjangoTestProvider\n\nopenfeature.api.set_provider(DjangoTestProvider())\n```\n\n`override_feature` is a function decorator that allows you to override the value of a feature flag for the duration of a test.\n\n```python\nfrom django_openfeature.test import override_feature\n\n@override_feature('my_feature', True)\ndef test_my_feature_enabled(self):\n    # ...\n```\n\nDecorators can be stacked to override multiple feature flags.\n\nYou can also use `override_feature` as a context manager.\n\n```python\nfrom django_openfeature.test import override_feature\n\ndef test_my_feature_enabled(self):\n    with override_feature('my_feature', True):\n        # ...\n```\n\n## TODO\n\n * [ ] Add support for OpenFeature domains (requires SDK release)\n\n## License\n\nMIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffedericobond%2Fdjango-openfeature","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffedericobond%2Fdjango-openfeature","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffedericobond%2Fdjango-openfeature/lists"}