Django Constance

4.3.5 · active · verified Sun Apr 12

Django Constance is a Django app that enables dynamic settings management, allowing configuration values to be edited directly from the Django admin interface without requiring code changes or redeployments. It supports pluggable backends, including built-in Redis and database options, and is actively maintained with regular releases. The current version is 4.3.5.

Warnings

Install

Imports

Quickstart

Install django-constance with your chosen backend (e.g., Redis). Add `constance` to your `INSTALLED_APPS`. Define your dynamic settings using the `CONSTANCE_CONFIG` dictionary in your `settings.py`, specifying a default value and a help text. Then, access these settings anywhere in your Python code by importing `config` from `constance` and using attribute access (e.g., `config.SITE_NAME`). If using the database backend, remember to run `python manage.py migrate`.

import os

# settings.py
# Add 'constance' to your INSTALLED_APPS.
INSTALLED_APPS = [
    # ... other Django apps
    'constance',
    # 'unfold.contrib.constance', # If using Django Unfold, place before 'constance'
]

# Configure backend (example for Redis)
CONSTANCE_BACKEND = 'constance.backends.redisd.RedisBackend'
CONSTANCE_REDIS_CONNECTION = {
    'host': os.environ.get('REDIS_HOST', 'localhost'),
    'port': int(os.environ.get('REDIS_PORT', 6379)),
    'db': int(os.environ.get('REDIS_DB', 0)),
}

# Define your dynamic settings
CONSTANCE_CONFIG = {
    'SITE_NAME': ('My Awesome Site', 'The name of the website.'),
    'MAINTENANCE_MODE': (False, 'Enable or disable maintenance mode.'),
    'MAX_USERS': (1000, 'Maximum allowed users on the platform.'),
}

# In your Django view or anywhere in Python code:
from constance import config

def my_view(request):
    if config.MAINTENANCE_MODE:
        return HttpResponse("Site is under maintenance.")
    site_name = config.SITE_NAME
    max_users = config.MAX_USERS
    print(f"Current site name: {site_name}")
    print(f"Max users: {max_users}")
    # ... your view logic

# Run Django migrations if using the database backend
# python manage.py migrate

view raw JSON →