Django LaunchDarkly Flags
A Django + DRF integration for LaunchDarkly feature flags with built-in REST API support.
Installation
pip install launchdarkly-drf
Quick Start
1. Add to INSTALLED_APPS
# settings.py
INSTALLED_APPS = [
# ...
'launchdarkly_drf',
]
# Configure your LaunchDarkly SDK key
LAUNCHDARKLY_SDK_KEY = 'your-sdk-key-here'
# Optional: disable LaunchDarkly in tests
LAUNCHDARKLY_OFFLINE = False # Set to True in test settings
2. Use Feature Flags in Your Code
from launchdarkly_drf import has_feature, get_flag_value
def my_view(request):
# Boolean flags
if has_feature('new-ui-enabled', request=request):
return render_new_ui(request)
# String/number/JSON flags
theme = get_flag_value('ui-theme', request=request)
max_items = get_flag_value('max-items-per-page', request=request)
config = get_flag_value('feature-config', request=request)
3. (Optional) Add REST API Endpoint
Expose flags to your frontend:
# urls.py
from rest_framework.routers import DefaultRouter
from launchdarkly_drf import FeatureFlagViewSet
router = DefaultRouter()
router.register(r'feature-flags', FeatureFlagViewSet, basename='feature-flags')
urlpatterns = [
path('api/', include(router.urls)),
]
Now you can GET /api/feature-flags/ to retrieve all flags as JSON.
Usage Guide
Boolean Feature Flags
Use has_feature() for simple on/off toggles:
from launchdarkly_drf import has_feature
# With request (automatically extracts user context)
if has_feature('beta-features', request=request):
enable_beta_features()
# With custom context
if has_feature('beta-features', context={'key': 'user@example.com', 'custom': 'value'}):
enable_beta_features()
# Anonymous context (no targeting)
if has_feature('public-feature'):
show_public_feature()
Multi-Variant Flags
Use get_flag_value() for string, number, or JSON flags:
from launchdarkly_drf import get_flag_value
# String flags (e.g., A/B test variants)
variant = get_flag_value('ui-variant', request=request)
if variant == 'blue':
render_blue_theme()
elif variant == 'green':
render_green_theme()
# Number flags
max_retries = get_flag_value('max-retries', request=request)
# JSON flags
config = get_flag_value('feature-config', request=request)
timeout = config.get('timeout', 30)
Get All Flags
Retrieve all flags at once (useful for frontend):
from launchdarkly_drf import get_all_flag_values
flags = get_all_flag_values(request)
# Returns: {'flag-1': True, 'flag-2': 'variant-a', 'flag-3': 42, ...}
Testing
Use the patch_feature_flag decorator to mock flags in tests:
from launchdarkly_drf.testing import patch_feature_flag
from launchdarkly_drf import has_feature, get_flag_value
@patch_feature_flag('new-ui-enabled', True)
@patch_feature_flag('ui-theme', 'dark')
def test_my_feature():
assert has_feature('new-ui-enabled') is True
assert get_flag_value('ui-theme') == 'dark'
You can stack multiple decorators to patch multiple flags.
Advanced Configuration
Custom User Context
By default, the package extracts the user's email from request.user.email. You can provide custom context:
from launchdarkly_drf import has_feature
# Custom targeting
context = {
'key': 'user-123',
'email': 'user@example.com',
'custom': {
'plan': 'enterprise',
'region': 'us-west',
}
}
if has_feature('enterprise-feature', context=context):
show_enterprise_features()
How It Works
- Initialization: The LaunchDarkly client is initialized when Django starts (in
AppConfig.ready()) - Singleton Pattern: A singleton LaunchDarkly client instance is shared across your app
- User Context: User information is automatically extracted from drf requests for flag targeting
- Offline Mode: Set
LAUNCHDARKLY_OFFLINE=Truein test settings to disable LaunchDarkly connections
Support
Metadata
Release files for launchdarkly-drf 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| launchdarkly_drf-0.1.0.tar.gz | 5.7 kB | Without provenance | Details for launchdarkly_drf-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| launchdarkly_drf-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for launchdarkly_drf-0.1.0-py3-none-any.whl |
Total release size: 13.3 kB
Release files / launchdarkly_drf-0.1.0.tar.gz
| Download URL | launchdarkly_drf-0.1.0.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
58d66d522ecbf15448c4c3fe5fdf2183a4b850e255bee12629a64295afcf4d6d
|
|
BLAKE2b-256 checksum How to use checksums |
720f0d89c375a248902326e00c0a603dad80e82079af15f625fe6afb5fb5b512
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / launchdarkly_drf-0.1.0-py3-none-any.whl
| Download URL | launchdarkly_drf-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
754aad6976ccd3ba7ba7dc74db73dace56eb41bd9fc3af64e8dc7c7b17c0898c
|
|
BLAKE2b-256 checksum How to use checksums |
ddb3a11864ce9fcf53e2de78fe2bda7b7650365fb62b881dce74cff8c3d06255
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for launchdarkly-drf 0.1.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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.