Feature flags for Django projects

5.2.0 · active · verified Thu Apr 16

Django-Flags is an application designed specifically for Django, empowering developers to utilize feature flags to toggle functionality in both Django code and templates based on configurable conditions. It's actively maintained, with recent updates adding support for Django 6.0 and Python 3.13, and typically sees several releases per year addressing new Django/Python versions and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

To get started, add 'flags' to your `INSTALLED_APPS` and ensure `django.template.context_processors.request` is in your `TEMPLATES` context processors for request-aware conditions. Define your flags and their conditions in `settings.py` within the `FLAGS` dictionary. Then, use `flag_enabled` in your Python code, `{% load feature_flags %}` in templates, or `flagged_path` in `urls.py` to gate functionality. Finally, run `python manage.py migrate`.

import os

# settings.py
INSTALLED_APPS = [
    # ...
    'flags',
    # ...
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'OPTIONS': {
            'context_processors': [
                # ...
                'django.template.context_processors.request',
                # ...
            ],
        },
    },
]

FLAGS = {
    'MY_NEW_FEATURE': [
        {'condition': 'boolean', 'value': os.environ.get('ENABLE_MY_NEW_FEATURE', 'False').lower() == 'true'}
    ],
    'BETA_ACCESS': [
        {'condition': 'user', 'value': 'admin'},
        {'condition': 'parameter', 'value': 'beta', 'required': False} # Can be overridden by URL param `?beta=true`
    ]
}

# views.py
from django.http import HttpResponse
from flags.state import flag_enabled

def my_feature_view(request):
    if flag_enabled('MY_NEW_FEATURE', request=request):
        return HttpResponse("Welcome to the new feature!")
    return HttpResponse("Feature coming soon.")

# urls.py
from django.urls import path
from flags.urls import flagged_path
from . import views

urlpatterns = [
    path('home/', views.my_feature_view, name='home'),
    flagged_path('BETA_ACCESS', 'beta-page/', views.my_beta_view, name='beta_page')
]

# After adding to INSTALLED_APPS and settings, run migrations:
# python manage.py migrate

view raw JSON →