https://github.com/jkbrzt/django-settings-export
Access Django settings from templates the right way™
https://github.com/jkbrzt/django-settings-export
django django-application jakubroztocil python settings templates
Last synced: 6 months ago
JSON representation
Access Django settings from templates the right way™
- Host: GitHub
- URL: https://github.com/jkbrzt/django-settings-export
- Owner: jkbrzt
- License: other
- Created: 2014-09-14T17:19:09.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2023-08-26T13:54:14.000Z (about 2 years ago)
- Last Synced: 2025-03-28T18:09:21.180Z (6 months ago)
- Topics: django, django-application, jakubroztocil, python, settings, templates
- Language: Python
- Homepage: https://pypi.org/project/django-settings-export/
- Size: 65.4 KB
- Stars: 190
- Watchers: 5
- Forks: 19
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.rst
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# `django-settings-export`
[](https://pepy.tech/project/django-settings-export)
[](https://pepy.tech/project/django-settings-export)
[](https://github.com/jkbrzt/django-settings-export/actions)
[](https://coveralls.io/r/jkbrzt/django-settings-export?branch=master)
[](https://pypi.python.org/pypi/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 un-exported setting key from a template results in an exception.
## Installation
Install the package:
```bash
pip install django-settings-export
```Add `'django_settings_export.settings_export'` to template context processor list in your `settings.py`:
```python
TEMPLATES = [
{
# …
'OPTIONS': {
'context_processors': [
# …
'django_settings_export.settings_export',
],
},
},
]
```[Tested on Python 3.8+, Django 2.2+](.github/workflows/test.yml)
## Usage
All settings that should be made accessible from templates need to be
explicitly listed in `settings.SETTINGS_EXPORT`:```python
# settings.pyDEBUG = True
GA_ID = 'UA-00000-0'SETTINGS_EXPORT = [
'DEBUG',
'GA_ID',
]
```Now you can access those exported settings from your templates
via `settings.`:```html
{% if not settings.DEBUG %}
ga('create', '{{ settings.GA_ID }}', 'auto');
{% 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:```html
{% 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.```python
# settings.py
FOO = 'bar'
SETTINGS_EXPORT = ['FOO']
SETTINGS_EXPORT_VARIABLE_NAME = 'my_config'
``````html
{{ my_config.FOO }}
```### Exceptions
These custom exceptions can be thrown:
* Listing an undefined setting key in `SETTINGS_EXPORT` results in an `UndefinedSettingError`.
* Accessing an un-exported 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](https://github.com/jkbrzt/django-settings-export/tree/master/tests).
## Development
```bash
$ cd tests# Run demo
$ python manage.py runserver# Run tests on current Python
$ python manage.py test
```## Change Log
See [`CHANGELOG`](https://github.com/jkbrzt/django-settings-export/blob/master/CHANGELOG.rst).
## Licence
BSD. See [`LICENCE`](https://github.com/jkbrzt/django-settings-export/tree/master/LICENCE) for more details.
## Contact
Jakub Roztocil
* http://roztocil.co
* https://github.com/jkbrzt
* https://twitter.com/jkbrzt