Django Integration Guide

Django Feature Flags

Feature flags for Django — template tags, middleware, and safe rollouts without django-waffle

Quick Start

1. Get your API key

Sign up at flagbit.anethoth.com to get your SDK key. The free tier includes 1,000 evaluations/day.

2. Install (optional)

pip install requests

3. Evaluate flags

import requests
from django.core.cache import cache
from functools import wraps

FLAGBIT_SDK_KEY = os.environ['FLAGBIT_SDK_KEY']

def get_flag(flag_key, context=None):
    cache_key = f"flag:{flag_key}:{hash(str(context))}"
    cached = cache.get(cache_key)
    if cached is not None:
        return cached

    try:
        resp = requests.post(
            'https://flagbit.anethoth.com/api/v1/evaluate',
            json={"flag_key": flag_key, "context": context or {}},
            headers={"X-SDK-Key": FLAGBIT_SDK_KEY},
            timeout=5
        )
        value = resp.json().get("value", False)
    except Exception:
        value = False

    cache.set(cache_key, value, timeout=60)
    return value

# View decorator
def feature_required(flag_key):
    def decorator(view_func):
        @wraps(view_func)
        def wrapper(request, *args, **kwargs):
            ctx = {}
            if request.user.is_authenticated:
                ctx = {"user_id": str(request.user.id)}
            if not get_flag(flag_key, ctx):
                raise Http404
            return view_func(request, *args, **kwargs)
        return wrapper
    return decorator

# Usage
@feature_required('new-dashboard')
def beta_dashboard(request):
    return render(request, 'dashboard_v2.html')

# Template tag — {% load feature_flags %}  {% feature 'banner' %}...{% endfeature %}
from django import template
register = template.Library()

@register.tag('feature')
def do_feature(parser, token):
    flag_key = token.split_contents()[1].strip("'\"")
    nodelist = parser.parse(('endfeature',))
    parser.delete_first_token()
    return FeatureNode(flag_key, nodelist)

class FeatureNode(template.Node):
    def __init__(self, flag_key, nodelist):
        self.flag_key, self.nodelist = flag_key, nodelist
    def render(self, context):
        request = context.get('request')
        ctx = {"user_id": str(request.user.id)} if request and request.user.is_authenticated else {}
        return self.nodelist.render(context) if get_flag(self.flag_key, ctx) else '' 

Use Cases for Django

Django Admin

Gate new admin features behind flags — test with staff users before rolling out to all admins.

Template Rendering

Use template tags to conditionally show UI elements — no view logic needed.

Management Commands

Flag-gate expensive management commands during high-traffic periods.

DRF Permissions

Create a custom DRF permission class that checks feature flags before allowing API access.

How FlagBit Works

1

Create a Flag

Define flags in your FlagBit dashboard with targeting rules and rollout percentages.

2

Evaluate in Code

Call the evaluate endpoint from your Django app with user context for targeted rollouts.

3

Toggle Instantly

Enable, disable, or adjust rollouts in real-time. No redeployment needed.

FAQ

FlagBit is hosted — no database tables, no migrations, no middleware to configure. Trade-off: external API call vs. local DB query.

Yes — create a custom permission class or use the decorator on DRF views.

Mock the get_flag function with unittest.mock.patch. Test both True and False return values.

Call get_flag from consumers — it's just a function call. Cache results to avoid API calls per WebSocket message.

Start using feature flags in Django

Free tier includes 1 project, 10 flags, and 1,000 evaluations/day. No credit card required.

Get Your Free API Key