Skip to main content

django-javascript-settings is a Django application that provides a way of passing settings for Django applications to JavaScript.

Project description

https://travis-ci.org/pozytywnie/django-javascript-settings.svg

javascript-settings is a Django application that provides a way of passing values backend from Django applications to JavaScript in templates.

Installation

Package

javascript-settings can be installed as a normal Python package.

Example instalation for pip:

$ pip install django-javascript-settings

Example instalation from file:

$ pip install django-javascript-settings-1.1.tar.gz

Configuration

settings.py

Add javascript-settings to INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'javascript_settings',
    ...
)

Add javascript-settings.finders to STATICFILES_FINDERS:

STATICFILES_FINDERS = (
    ...
    'javascript_settings.finders.JavascriptSettingsFinder',
    ...
)

template

You can use one of following ways to pass gathered data to template:

Option 1: Import

Add javascript-settings.js to script imports:

<script type="text/javascript" src="{{ STATIC_URL }}javascript-settings.js"></script>

Option 2: Template tag

Add javascript-settings tag to your main template:

{% load javascript_settings_tags %}
<script type="text/javascript">{% javascript_settings %}</script>

Usage

Configuration is defined by adding javascript_settings function to urls.py of app. javascript_settings should take no arguments and return json-serializable object. Serialized object is then avaliable in Javascript as configuration['app_name'].

If you want to place javascript_settings in different location, then you can define JAVASCRIPT_SETTINGS_SCAN_MODULES as a dictionary of 'app_name': 'module_location'.

Example

Template:

<script type="text/javascript" src="{{ STATIC_URL }}javascript-settings.js"></script>

urls.py in an app “home”:

def javascript_settings():
    return {
        'page_title': 'Home',
        'page_version': '1.9.20',
        'css': {
            'white': './css/white.css',
            'black': './css/black.css',
            'print': './css/print.css',
        },
        'default_css': 'white',
    }

Result in file javascript-settings.js:

var configuration = {'home': {'page_title': 'Home', 'page_version': '1.9.20', 'css': {'white': './css/white.css', 'black': './css/black.css', 'print': './css/print.css'}, 'default_css': 'white'}};

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-javascript-settings-1.1.2.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file django-javascript-settings-1.1.2.tar.gz.

File metadata

File hashes

Hashes for django-javascript-settings-1.1.2.tar.gz
Algorithm Hash digest
SHA256 33c0ae7ce0cf405f81d29e80924b37705942ff68e344a24b930d8927234d390e
MD5 ddc6409a492fedfb0fa1521ca0624a73
BLAKE2b-256 19d8db8c74f640ce278cc16854b06f8c52801cd4552f1c502110bb28123423b2

See more details on using hashes here.

Provenance

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