django-configurations
A helper for organizing Django project settings by relying on well established programming patterns.
Top Related Projects
The Web framework for perfectionists with deadlines.
Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly.
Bleeding edge django template focused on code quality and security.
Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application.
Strict separation of config from code.
Quick Overview
Django-configurations is a Python package that extends Django's configuration system. It allows developers to organize settings into classes, providing a more structured and modular approach to managing Django project configurations. This library enhances the flexibility and reusability of Django settings across different environments.
Pros
- Improves organization and readability of Django settings
- Enables easy inheritance and overriding of configuration classes
- Supports environment-specific settings management
- Integrates well with Django's existing configuration system
Cons
- Adds an extra layer of complexity to the Django setup process
- May require a learning curve for developers unfamiliar with class-based configurations
- Limited community support compared to Django's built-in settings approach
- Potential for conflicts with other Django packages that expect traditional settings
Code Examples
- Basic Configuration Class:
from configurations import Configuration
class Base(Configuration):
DEBUG = True
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
# ... other apps
]
- Environment-specific Configuration:
from .base import Base
class Production(Base):
DEBUG = False
ALLOWED_HOSTS = ['example.com']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'production_db',
# ... other database settings
}
}
- Using Environment Variables:
import os
from configurations import Configuration, values
class Staging(Configuration):
SECRET_KEY = values.SecretValue()
DATABASE_URL = values.DatabaseURLValue(
environ_prefix='MYAPP',
environ_name='DATABASE_URL',
default='sqlite:///db.sqlite3'
)
Getting Started
-
Install django-configurations:
pip install django-configurations
-
Modify your Django project's
manage.py
:#!/usr/bin/env python import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") os.environ.setdefault("DJANGO_CONFIGURATION", "Dev") from configurations.management import execute_from_command_line execute_from_command_line(sys.argv)
-
Create a
settings.py
file with your configuration classes:from configurations import Configuration class Base(Configuration): # Common settings here class Dev(Base): DEBUG = True class Prod(Base): DEBUG = False
-
Run Django commands specifying the configuration:
DJANGO_CONFIGURATION=Dev python manage.py runserver
Competitor Comparisons
The Web framework for perfectionists with deadlines.
Pros of Django
- Comprehensive web framework with built-in features for rapid development
- Large, active community with extensive documentation and third-party packages
- Robust ORM for database operations and migrations
Cons of Django
- Configuration can be complex and less flexible out-of-the-box
- Steeper learning curve for beginners due to its full-stack nature
- Monolithic structure may be overkill for smaller projects
Code Comparison
Django (settings.py):
DEBUG = True
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
django-configurations:
from configurations import Configuration
class Dev(Configuration):
DEBUG = True
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
django-configurations offers a more modular and class-based approach to Django settings, allowing for easier management of multiple environments and configurations. It provides a cleaner separation of concerns and improved readability, especially for complex projects with multiple settings files.
Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly.
Pros of cookiecutter-django
- Provides a complete project structure with best practices out of the box
- Includes optional integrations for various tools and services
- Offers flexibility in choosing project components during setup
Cons of cookiecutter-django
- Can be overwhelming for beginners due to its comprehensive nature
- Requires more setup time compared to simpler configuration options
- May include unnecessary components for smaller projects
Code Comparison
cookiecutter-django:
# config/settings/base.py
from pathlib import Path
import environ
ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
APPS_DIR = ROOT_DIR / "{{ cookiecutter.project_slug }}"
env = environ.Env()
django-configurations:
# settings.py
from configurations import Configuration
class Dev(Configuration):
DEBUG = True
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'dev.db',
}
}
cookiecutter-django provides a more structured approach with separate settings files and environment variable handling, while django-configurations focuses on class-based configuration management. The choice between them depends on project complexity and personal preference for configuration style.
Bleeding edge django template focused on code quality and security.
Pros of wemake-django-template
- Comprehensive project structure with best practices and tools pre-configured
- Includes Docker setup for easier deployment and development
- Provides a complete CI/CD pipeline configuration
Cons of wemake-django-template
- More opinionated and less flexible than django-configurations
- Steeper learning curve due to the extensive tooling and structure
- May be overkill for smaller projects or those with specific requirements
Code Comparison
django-configurations:
from configurations import Configuration
class Dev(Configuration):
DEBUG = True
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'dev.db',
}
}
wemake-django-template:
from server.settings.components import config
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': config('POSTGRES_DB'),
'USER': config('POSTGRES_USER'),
'PASSWORD': config('POSTGRES_PASSWORD'),
'HOST': config('DJANGO_DATABASE_HOST'),
},
}
The code comparison shows that django-configurations focuses on class-based configuration, while wemake-django-template uses a more structured approach with separate configuration files and environment variables.
Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application.
Pros of django-environ
- Simpler setup and usage, requiring fewer code changes
- Better integration with environment variables and .env files
- More lightweight and focused on configuration management
Cons of django-environ
- Less flexibility in defining complex configuration classes
- Fewer advanced features for managing different environments
- Limited support for custom configuration types
Code Comparison
django-environ:
import environ
env = environ.Env()
environ.Env.read_env()
DEBUG = env.bool('DEBUG', default=False)
SECRET_KEY = env('SECRET_KEY')
django-configurations:
from configurations import Configuration
class Dev(Configuration):
DEBUG = True
SECRET_KEY = 'your-secret-key'
class Prod(Configuration):
DEBUG = False
SECRET_KEY = os.environ['SECRET_KEY']
django-environ focuses on reading configuration from environment variables and .env files, making it easier to manage different environments without changing code. It's more straightforward for simple projects but may lack some advanced features.
django-configurations provides a class-based approach to configuration, allowing for more complex setups and inheritance between different environments. It offers greater flexibility but requires more code changes and may be overkill for smaller projects.
Both libraries aim to improve Django's configuration management, but they take different approaches to achieve this goal.
Strict separation of config from code.
Pros of python-decouple
- Simpler and more lightweight, focusing solely on configuration management
- Can be used in any Python project, not limited to Django applications
- Provides easy-to-use methods for casting configuration values to specific types
Cons of python-decouple
- Lacks advanced features like class-based configurations and inheritance
- Does not provide built-in support for environment-specific settings
- May require additional setup for complex configuration scenarios
Code Comparison
python-decouple:
from decouple import config
DEBUG = config('DEBUG', default=False, cast=bool)
SECRET_KEY = config('SECRET_KEY')
DATABASE_URL = config('DATABASE_URL')
django-configurations:
from configurations import Configuration
class Dev(Configuration):
DEBUG = True
SECRET_KEY = 'your-secret-key'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'dev.db',
}
}
python-decouple offers a straightforward approach to managing configuration variables, while django-configurations provides a more structured, class-based method for organizing settings. The choice between the two depends on the project's complexity and specific requirements.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
django-configurations |latest-version|
|jazzband| |build-status| |codecov| |docs| |python-support| |django-support|
django-configurations eases Django project configuration by relying on the composability of Python classes. It extends the notion of Django's module based settings loading with well established object oriented programming patterns.
Check out the documentation
_ for more complete examples.
.. |latest-version| image:: https://img.shields.io/pypi/v/django-configurations.svg :target: https://pypi.python.org/pypi/django-configurations :alt: Latest version on PyPI
.. |jazzband| image:: https://jazzband.co/static/img/badge.svg :target: https://jazzband.co/ :alt: Jazzband
.. |build-status| image:: https://github.com/jazzband/django-configurations/workflows/Test/badge.svg :target: https://github.com/jazzband/django-configurations/actions :alt: Build Status
.. |codecov| image:: https://codecov.io/github/jazzband/django-configurations/coverage.svg?branch=master :target: https://codecov.io/github/jazzband/django-configurations?branch=master :alt: Test coverage status
.. |docs| image:: https://img.shields.io/readthedocs/django-configurations/latest.svg :target: https://readthedocs.org/projects/django-configurations/ :alt: Documentation status
.. |python-support| image:: https://img.shields.io/pypi/pyversions/django-configurations.svg :target: https://pypi.python.org/pypi/django-configurations :alt: Supported Python versions
.. |django-support| image:: https://img.shields.io/pypi/djversions/django-configurations :target: https://pypi.org/project/django-configurations :alt: Supported Django versions
.. _documentation: https://django-configurations.readthedocs.io/en/latest/
Quickstart
Install django-configurations:
.. code-block:: console
$ python -m pip install django-configurations
or, alternatively, if you want to use URL-based values:
.. code-block:: console
$ python -m pip install django-configurations[cache,database,email,search]
Then subclass the included configurations.Configuration
class in your
project's settings.py or any other module you're using to store the
settings constants, e.g.:
.. code-block:: python
# mysite/settings.py
from configurations import Configuration
class Dev(Configuration):
DEBUG = True
Set the DJANGO_CONFIGURATION
environment variable to the name of the class
you just created, e.g. in bash:
.. code-block:: console
$ export DJANGO_CONFIGURATION=Dev
and the DJANGO_SETTINGS_MODULE
environment variable to the module
import path as usual, e.g. in bash:
.. code-block:: console
$ export DJANGO_SETTINGS_MODULE=mysite.settings
Alternatively supply the --configuration
option when using Django
management commands along the lines of Django's default --settings
command line option, e.g.
.. code-block:: console
$ python -m manage runserver --settings=mysite.settings --configuration=Dev
To enable Django to use your configuration you now have to modify your manage.py, wsgi.py or asgi.py script to use django-configurations's versions of the appropriate starter functions, e.g. a typical manage.py using django-configurations would look like this:
.. code-block:: python
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
os.environ.setdefault('DJANGO_CONFIGURATION', 'Dev')
from configurations.management import execute_from_command_line
execute_from_command_line(sys.argv)
Notice in line 10 we don't use the common tool
django.core.management.execute_from_command_line
but instead
configurations.management.execute_from_command_line
.
The same applies to your wsgi.py file, e.g.:
.. code-block:: python
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
os.environ.setdefault('DJANGO_CONFIGURATION', 'Dev')
from configurations.wsgi import get_wsgi_application
application = get_wsgi_application()
Here we don't use the default django.core.wsgi.get_wsgi_application
function but instead configurations.wsgi.get_wsgi_application
.
Or if you are not serving your app via WSGI but ASGI instead, you need to modify your asgi.py file too.:
.. code-block:: python
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
os.environ.setdefault('DJANGO_CONFIGURATION', 'Dev')
from configurations.asgi import get_asgi_application
application = get_asgi_application()
That's it! You can now use your project with manage.py
and your favorite
WSGI/ASGI enabled server.
Top Related Projects
The Web framework for perfectionists with deadlines.
Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly.
Bleeding edge django template focused on code quality and security.
Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application.
Strict separation of config from code.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot