Skip to main content

django-openfeature

build status release python versions

⚠️ Caution: this repository is a work-in-progress. ⚠️

Django OpenFeature is a set of utilities to use OpenFeature in your Django applications.

Features

  • Django Debug Toolbar integration
  • Templatetags for flag evaluation
  • Automatic evaluation context from request
  • Flag override mechanism for testing

Installation

pip install django-openfeature

Add django_openfeature to your INSTALLED_APPS setting.

INSTALLED_APPS = [
    # ...
    'django_openfeature',
    # ...
]

Usage

Flag Evaluation Helpers

django-openfeature provides a set of helpers to evaluate a feature flag:

from django_openfeature import feature

feature(request, 'my_feature', False)

The feature function will infer the type of the feature flag based on the default value provided and call the appropriate resolver method.

It will also create an evaluation context from the request. This context can be configured via the OPENFEATURE setting (described below).

Inside templates, you can use the feature template tag to evaluate feature flags. Remember to load the openfeature library first.

{% load openfeature %}

{% feature 'my_feature' False as my_feature_enabled %}
{% if my_feature_enabed %}
    <p>Feature is enabled</p>
{% else %}
    <p>Feature is disabled</p>
{% endif %}

As an alternative to evaluating feature flags to a variable you can use the iffeature template tag. It will output the contents of the block if the boolean flag resolves to True. It supports a single {% else %} clause that will be displayed otherwise.

{% load openfeature %}

{% iffeature 'my_feature' %}
    <p>Feature is enabled</p>
{% else %}
    <p>Feature is disabled</p>
{% endif %}

Configuration

OPENFEATURE = {
    "PROVIDER": "myapp.utils.get_openfeature_provider",
    "CONTEXT_EVALUATOR": "myapp.utils.get_evaluation_context",
}

The PROVIDER setting configures the global OpenFeature provider when the app registry loads, saving you from calling openfeature.api.set_provider() yourself at import time. It should point to a provider class, a zero-argument factory function that returns a provider instance, or a provider instance. When omitted (or None), django-openfeature does not touch the provider and you can keep setting it manually.

# myapp/utils.py
from devcycle_python_sdk import DevCycleCloudClient, DevCycleCloudOptions
from django.conf import settings

def get_openfeature_provider():
    client = DevCycleCloudClient(settings.DEVCYCLE_API_KEY, DevCycleCloudOptions())
    return client.get_openfeature_provider()

The provider is re-configured when the OPENFEATURE setting changes, so override_settings(OPENFEATURE={"PROVIDER": ...}) works as expected in tests.

The CONTEXT_EVALUATOR setting should point to a function that receives a request and returns an OpenFeature EvaluationContext as defined in the openfeature-sdk package.

Alternatively, 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:

class OpenFeatureContext:
    targeting_key = "id"
    targeting_key_anonymous = "unknown"
    user_attributes = ("username", "email")

    def get_context(self, request):
        user = request.user
        if not user or user.is_anonymous:
            return EvaluationContext(self.targeting_key_anonymous)
        return self.get_user_context(user)

    def get_user_context(self, user):
        attributes = {}
        for attr in self.user_attributes:
            attributes[attr] = getattr(user, attr)
        targeting_key = str(getattr(user, self.targeting_key))
        return EvaluationContext(targeting_key, attributes)

    def __call__(self, request):
        return self.get_context(request)

Use this class as base to declaratively define your own context evaluator. For example, to change the targeting key to the user's username:

from django_openfeature.context import OpenFeatureContext

class MyOpenFeatureContext(OpenFeatureContext):
    targeting_key = "username"

Then in your settings:

OPENFEATURE = {
    "CONTEXT_EVALUATOR": "myapp.utils.MyOpenFeatureContext",
}

Debug Toolbar Panel

django-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.

DEBUG_TOOLBAR_PANELS = [
    # ...
    'openfeature.debug_toolbar.panels.FeatureFlagsPanel',
    # ...
]

The Feature Flags panel will show you the feature flag evaluations for the current request, the request evaluation context and the configured providers for those evaluations.

Feature Flags Debug Toolbar Panel

Testing Utilities

django-openfeature provides a set of utilities to help you test your feature flags. To use them, you must set the provider to django_openfeature.provider.DjangoTestProvider in your test settings.

# in your test settings
OPENFEATURE = {
    "PROVIDER": "django_openfeature.provider.DjangoTestProvider",
}

override_feature is a function decorator that allows you to override the value of a feature flag for the duration of a test.

from django_openfeature.test import override_feature

@override_feature('my_feature', True)
def test_my_feature_enabled(self):
    # ...

Decorators can be stacked to override multiple feature flags.

You can also use override_feature as a context manager.

from django_openfeature.test import override_feature

def test_my_feature_enabled(self):
    with override_feature('my_feature', True):
        # ...

TODO

  • Add support for OpenFeature domains (requires SDK release)

License

MIT License

Metadata

Release files for django-openfeature 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-openfeature 0.2.0
File Size Uploaded Provenance Actions
django_openfeature-0.2.0.tar.gz 13.6 kB With provenance Details for django_openfeature-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-openfeature 0.2.0
File Interpreter ABI Platform Actions
django_openfeature-0.2.0-py3-none-any.whl Python 3 none any Details for django_openfeature-0.2.0-py3-none-any.whl

Total release size: 25.5 kB

Release files / django_openfeature-0.2.0.tar.gz

Download URL django_openfeature-0.2.0.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d91ad356c58876db0adce01b0da1e78a0d2a9e4fd2dbbb113e1ec21375d2eda8
BLAKE2b-256 checksum
How to use checksums
bccd8f85f00aac1e9ed02660b0340dda7d43a1fe37071e2a1699b541066534e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 5, 2026.

Transparency log

Release files / django_openfeature-0.2.0-py3-none-any.whl

Download URL django_openfeature-0.2.0-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
05a5ae7ad1cc6f9690a930c79b6c47226d8f5cb1c76bf53359ebd4f7be42e5ba
BLAKE2b-256 checksum
How to use checksums
b7dabc878b497a40332bbb21e297e7fed26b567025a3069ca9a25d99c9bf1b4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 5, 2026.

Transparency log

Security for django-openfeature 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_openfeature-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 5, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page