1.2.1 - This version may not be safe as it has not been updated for a long time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform
Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.
BSD - BSD License (Generic)Often it is needed to make some of your Django project's settings accessible from within templates. This app provides a simple mechanism for doing just that.
Only explicitly listed settings keys are exported to templates.
Accessing an undefined or un-exported setting key from a template results in an exception.
Install the package:
pip install django-settings-exportAdd 'django_settings_export.settings_export' to template context processor list in your settings.py:
TEMPLATES = [
{
# …
'OPTIONS': {
'context_processors': [
# …
'django_settings_export.settings_export',
],
},
},
]Tested on Python 3.8+, Django 2.2+
All settings that should be made accessible from templates need to be
explicitly listed in settings.SETTINGS_EXPORT:
# settings.py
DEBUG = True
GA_ID = 'UA-00000-0'
SETTINGS_EXPORT = [
'DEBUG',
'GA_ID',
]Now you can access those exported settings from your templates
via settings.<KEY>:
<!-- template.html -->
{% if not settings.DEBUG %}
<script>ga('create', '{{ settings.GA_ID }}', 'auto');</script>
{% endif %}The settings variable is an instance of dict subclass, so
you use all the methods dict provides. For example, you can iterate over
the keys and values using settings.keys, settings.values,
settings.items, etc:
{% for key, value in settings.items %}
{{ key }}: {{ value }}
{% endfor %}If you wish to change the name of the context variable to something besides
settings, add SETTINGS_EXPORT_VARIABLE_NAME = 'custom_name'
to your settings.py. This is useful when some other plugin is already adding
settings to your template contexts.
# settings.py
FOO = 'bar'
SETTINGS_EXPORT = ['FOO']
SETTINGS_EXPORT_VARIABLE_NAME = 'my_config'<!-- template.html -->
{{ my_config.FOO }}These custom exceptions can be thrown:
SETTINGS_EXPORT results in an UndefinedSettingError.settings object in a template results in an UnexportedSettingError.All subclass from django_settings_export.SettingsExportError.
See the source code of the bundled demo app.
$ cd tests
# Run demo
$ python manage.py runserver
# Run tests on current Python
$ python manage.py testSee CHANGELOG.
BSD. See LICENCE for more details.
Jakub Roztocil