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
Django/Python Consulting
If you have a specific Django challenge or integration you'd like to solve, I'd be happy to help. Book a free 30-minute call to discuss your project, see if we're a good fit, and explore the best approach for your needs. After the call, you'll receive a tailored cost estimate based on what we discuss.
Also by me
Django Messaging
For Django-based social platforms.
Django Paddle Subscriptions
For Django-based SaaS projects.
Django GDPR Cookie Consent
For Django websites that use cookies.