Skip to main content

This Django app allows you to export certain settings to your templates.

Project description

PyPi Build Status of the master branch Coverage

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>:

{% if not settings.DEBUG %}
    <script>ga('create', '{{ settings.GA_ID }}', 'auto');</script>
{% endif %}

Exceptions:

  • 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.

See also the bundled demo app.

Development

$ cd demo

# Run demo
$ python manage.py runserver

# Run tests on current Python
$ python manage.py test

# Run tests on all Pythons
$ tox

Licence

BSD. See LICENCE for more details.

Contact

Jakub Roztocil

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-settings-export-1.0.5.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

django_settings_export-1.0.5-py2-none-any.whl (5.3 kB view details)

Uploaded Python 2

File details

Details for the file django-settings-export-1.0.5.tar.gz.

File metadata

File hashes

Hashes for django-settings-export-1.0.5.tar.gz
Algorithm Hash digest
SHA256 9882af82fdfa43926d8ea73426f277a347c2fd0795a7d0e95eff6aee2e6539b7
MD5 b2914c53a55111b7417e87445402a428
BLAKE2b-256 6e1a39d70e43849a9edd2ed781a9ce0b773a6a80af382054aa253293673e5afd

See more details on using hashes here.

File details

Details for the file django_settings_export-1.0.5-py2-none-any.whl.

File metadata

File hashes

Hashes for django_settings_export-1.0.5-py2-none-any.whl
Algorithm Hash digest
SHA256 2b802c502c56ce317ea33a0e03521402acecf3d228ddbafe81a5cde70ab90254
MD5 e1b0c9db2e2354c43463bd7a3f5c1da2
BLAKE2b-256 7889b282b0baad886662a3362defa03305d61c13ca4a2ac3702c04b315e80637

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page