django-settings-export
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.
Principles:
Explicit is better than implicit: Only explicitly listed settings keys are exported to templates.
Errors should never pass silently: Accessing an undefined or unexported setting key from a template results in an exception.
Tested on Python 2.7+, Django 1.5+.
Installation
$ pip install django-settings-export
Add 'django_settings_export.settings_export' to template context processor list in your settings.py:
Django 1.8 and newer:
TEMPLATES = [
{
# …
'OPTIONS': {
'context_processors': [
# …
'django_settings_export.settings_export',
],
},
},
]
Django older than 1.8:
TEMPLATE_CONTEXT_PROCESSORS = [
# [...]
'django_settings_export.settings_export',
]
Usage
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 %}
Changing the settings variable name
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 }}
Exceptions
These custom exceptions can be thrown:
Listing an undefined setting key in SETTINGS_EXPORT results in an UndefinedSettingError.
Accessing a unexported setting key on the settings object in a template results in an UnexportedSettingError.
All subclass from django_settings_export.SettingsExportError.
Demo & Tests
See the source code of the bundled demo app.
Development
$ cd tests
# Run demo
$ python manage.py runserver
# Run tests on current Python
$ python manage.py test
# Run tests on all Pythons
$ tox
Change Log
See CHANGELOG.
Licence
BSD. See LICENCE for more details.
Contact
Jakub Roztocil
Metadata
Release files for django-settings-export 1.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-settings-export-1.2.1.tar.gz | 5.0 kB | Without provenance | Details for django-settings-export-1.2.1.tar.gz |
Release files / django-settings-export-1.2.1.tar.gz
| Download URL | django-settings-export-1.2.1.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fceeae49fc597f654c1217415d8e049fc81c930b7154f5d8f28c432db738ff79
|
|
BLAKE2b-256 checksum How to use checksums |
d6729848a2d631dad70d7ea582540f0619e1a7ecf31b3a117de9d9f2b6b28029
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django-settings-export 1.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.