This package allows the apps to set its own settings and search for them in the project with the specified prefix. This approach makes it convenient to implement app settings in any project with classic way and not spend time to write your own settings module.
In Adding to your app section described the way how you can use this package in your own app.
In Project level usage section you will be informed about what you need to tell to your app users to set the settings correct.
- Clone repo:
git clone https://github.com/1mace1/django-app-settings.git
- Change directory:
cd django-app-settings/
- Make a package:
python setup.py sdist
- Install with pip:
pip install dist/django-app-settings-*.tar.gz
- Add django-app-settings to your package requirements
- Import
Settings
in to your app settings.py:
from django_app_settings import Settings
- Set the settings names that are required in your app:
settings = (
'SETTING1',
'SETTING2',
...
)
Warning
All the settings names should be uppercase!
- (optional) Set
defaults
for your settings so that the setting can fall back to defaults if it is not found in project:
defaults = {
'SETTING1': 'value',
...
}
- (optional) Set the
app_prefix
in order to be able to find your settings in the project using this prefix:
app_prefix = 'MYAPP'
Note
This is an optional step, so you don't have to set app_prefix, but I strongly recommend you do this so that there are no conflicts with other project settings
- (optional) If you have constant settings that don't need to be changed, you can use internal_settings. User at the project level will not have access to these settings:
internal_settings = {
'INTERNAL1': 'VALUE1',
...
}
- Create
Settings
object:
app_settings = Settings(settings, app_prefix=app_prefix, defaults=defaults, internal_settings=internal_settings)
Summing up, your settings.py should be:
from django_app_settings import Settings
app_prefix = 'MY_APP'
settings = (
'SETTING1',
'SETTING2'
)
defaults = {
'SETTING1': 'value'
}
internal_settings = {
'INTERNAL1': 'VALUE1',
}
app_settings = Settings(settings, app_prefix=app_prefix, defaults=defaults, internal_settings=internal_settings)
You can skip the optional steps and set the settings
only so that your app_settings
are like this:
from django_app_settings import Settings
settings = (
'SETTING1',
'SETTING2'
)
app_settings = Settings(settings)
For now, you can import app_settings
anywhere and use as below
from your_app.settings import app_settings
app_settings.SETTING1
All the settings in settings
tuple are mandatory. So if default value for particular setting are not specified and there is no setting in project level, the user will get an error.
Therefore, please, warn users of your app to set the required settings as shown below in Project level usage.
People who will use your app with this package should set settings in their project level settings.py with your app_prefix
or just leave them.
So if your app_prefix
is SOME_APP
as above then project level settings should be:
MY_APP_SETTING1 = 'some value'
MY_APP_SETTING2 = 'another value'