For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
13.5kProductPricingSign inStart free trialBook a demo
DocsAPIsSDKsEnterprise EdgeGuidesAcademyRelease notes
DocsAPIsSDKsEnterprise EdgeGuidesAcademyRelease notes
    • Overview
  • Feature management best practices
    • Building and scaling feature flag systems
    • Using feature flags at scale
    • Migrating from homegrown feature management solutions
    • Managing large constraints
  • Language and framework examples
      • Python
      • Advanced Python examples
      • Django
      • Advanced Django examples
    • Next.js
    • JavaScript
    • Serverless
    • Flutter
    • SvelteKit

Unleash reduces the risk of releasing new features, drives innovation by streamlining the software release process, and increases revenue by optimizing end-user experience. While we serve the needs of the world's largest, most security-conscious organizations, we are also rated the “Easiest Feature Management system to use” by G2.

GitHubGitHubLinkedInLinkedInX (Twitter)X (Twitter)SlackSlackStack OverflowStack OverflowYouTubeYouTube

Server SDKs

  • Node.js
  • Java
  • Go
  • Rust
  • Ruby
  • Python
  • .NET
  • PHP
  • All SDKs

Frontend SDKs

  • JavaScript
  • React
  • Next.js
  • Vue
  • iOS
  • Android
  • Flutter

Feature Flag use cases

  • Secure, scalable feature flags
  • Rollbacks
  • FedRAMP, SOC2, ISO2700 compliance
  • Progressive or gradual rollouts
  • Trunk-based development
  • Software kill switches
  • A/B testing
  • Feature management
  • Canary releases

Product

  • Quickstart
  • Unleash architecture
  • Pricing
  • Product vision
  • Open live demo
  • Open source
  • Enterprise feature management platform
  • Unleash vs LaunchDarkly

Support

  • Help center
  • Status
  • Changelog
Made in a cosy atmosphere in the Nordic countries.Copyright © 2026 Unleash
LogoLogo
13.5kProductPricingSign inStart free trialBook a demo
On this page
  • Prerequisites
  • 1. Install a local feature flag provider
  • 2. Set up the Django app
  • 3. Restrict the number of posts
  • 4. Add Unleash to your Django app
  • 6. Verify the toggle experience
  • Conclusion
Language and framework examplesPython

How to Implement Feature Flags in Django

||View as Markdown|
Was this page helpful?

Last updated May 11, 2026

Previous

Django Feature Flag Examples

Next
Built with

Hello! In this tutorial, we’ll show you how to add feature flags to your Django app, using Unleash and the official Unleash Python SDK. With Unleash, an open-source feature flag service, you can use our tooling to add feature flags to your application and release new features faster.

In a classic tutorial fashion, we’ll add feature flags to a blog app made with Django. We’ll use feature flags to decide how many blog posts to show on the index page.

Prerequisites

For this tutorial, you’ll need the following:

  • Python3.10+
  • Git
  • Docker and Docker Compose

architecture diagram for our implementation

The Unleash Server is a Feature Flag Control Service, which manages your feature flags and lets you retrieve flag data. Unleash has a UI for creating and managing projects and feature flags. For backend applications or automated scripts, Unleash exposes an API defined by an OpenAPI specification, allowing you to perform these actions programmatically.

1. Install a local feature flag provider

In this section, we’ll install Unleash, run the instance locally, log in, and create a feature flag. If you prefer, you can use other tools instead of Unleash, but you’ll need to update the code accordingly. The basic steps will probably be the same.

Use Git to clone the Unleash repository and Docker to build and run it. Open a terminal window and run the following commands:

git clone https://github.com/unleash/unleash.git
cd unleash
docker compose up -d

You will now have Unleash installed onto your machine and running in the background. You can access this instance in your web browser at http://localhost:4242.

Log in to the platform using these credentials:

Username: admin
Password: unleash4all

Click the ‘New feature flag’ button to create a new feature flag.

Create a new feature flag

Call it top-3 and enable it in the development environment.

A feature flag called top-3 is now visible.

Everything’s now set up on the Unleash side. Let’s set up the Django application.

2. Set up the Django app

Let’s clone a basic blog repository and get it up and running. We don’t want to waste time setting up a Django codebase from scratch.

1git clone https://github.com/alvinometric/django-basic-blog
2cd django-basic-blog

After that, set up a virtual environment and install Django.

1python3 -m venv venv
2source venv/bin/activate
3pip install django

Set up and seed the database

This repository uses SQLite, so no additional dependencies or migrations are required.

1python manage.py loaddata initial_data.json

Run the server

1python manage.py runserver

Go to http://localhost:8000 and check that you see the following:

A blog app with a list of posts

3. Restrict the number of posts

Right now all the blog posts are displayed on the index page. We want to use a feature flag to change that and restrict it to the 3 most recent posts.

Let’s create a static boolean flag, for now.

Modify the post_list view in blog/views.py to look like this:

1from django.shortcuts import render
2from .models import Post
3
4def post_list(request):
5 is_top3 = True
6 posts = Post.objects.order_by('-published_date')[:3] if is_top3 else Post.objects.all()
7 return render(request, 'blog/post_list.html', {'posts': posts})

We’re using the flag in the views rather than the template, but you could also do it there. I prefer keeping my template logic as simple as possible.

Reload your browser and you should see only the 3 most recent posts.

4. Add Unleash to your Django app

Now, let’s connect our project to Unleash so that you can toggle the feature flag at runtime. If you wanted to, you could also do a gradual rollout, and use it for A/B testing or more advanced functionality.

You’ll need 2 things:

  • The URL of your Unleash instance’s API. It’s http://localhost:4242/api/ for your local version. You’ll want to replace it with your remote instance.
  • The API token we created on our Unleash instance.

First, install the UnleashClient package:

1pip install UnleashClient

Then, create blog/unleash_client.py, and add the following:

1# You DO NOT want to do this in a production environment
2# Rather, you would create a singleton that is shared across your application
3from UnleashClient import UnleashClient
4
5unleash_client = UnleashClient(
6 url="http://localhost:4242/api/",
7 app_name="django-blog",
8 custom_headers={'Authorization': '<YOUR_API_TOKEN>'}
9)
10unleash_client.initialize_client()

Then, in blog/views.py, update the post_list view:

1from django.shortcuts import render
2from .models import Post
3from .unleash_client import unleash_client
4
5def post_list(request):
6 if unleash_client.is_enabled("top-3"):
7 posts = Post.objects.order_by('-published_date')[:3]
8 else:
9 posts = Post.objects.order_by('-published_date')
10 return render(request, 'blog/post_list.html', {'posts': posts})

6. Verify the toggle experience

Reload your browser and check that you see three blog posts displayed. Turn off the flag in your Unleash instance and reload the page. You should see all the blog posts again.

See additional use cases in our Python SDK documentation.

Conclusion

All done! Now you know how to add feature flags with Unleash in Django. You’ve learned how to:

  • Install Unleash
  • Create and enable a feature flag
  • Grab the value of a feature flag with the Python SDK, and use it in a Django app

Thank you for following this tutorial!