About a Pattern for Reusable App Configuration
Here is a simple pattern to app-specific configurations that can be overwritten in the project settings:
# myapp/conf.py
from django.conf import settings
_MYAPP_DEFAULTS = {
"USE_FOR_ANONYMOUS_USERS": False,
"USE_FOR_AUTHENTICATED_USERS": True,
}
class AppSettings:
def __getattr__(self, name):
return getattr(settings, "MYAPP", {}).get(name, _MYAPP_DEFAULTS[name])
app_settings = AppSettings()
Then you can use the app_settings
in your app as shown here:
from myapp.conf import app_settings
if app_settings.USE_FOR_ANONYMOUS_USERS:
print("Anonymous visitors will see this")
And you can change the settings per project as follows:
# myproject/settings/base.py
MYAPP = {
"USE_FOR_ANONYMOUS_USERS": True,
}
Tips and Tricks Programming Development Django 5.2 Django 4.2 Django 3.2 Python 3
Also by me
Django Paddle Subscriptions app
For Django-based SaaS projects.
Django GDPR Cookie Consent app
For Django websites that use cookies.