Skip to content

Instantly share code, notes, and snippets.

@fdobrovolny
Created January 5, 2017 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdobrovolny/632f56ddb907108b3d43fa862510dfca to your computer and use it in GitHub Desktop.
Save fdobrovolny/632f56ddb907108b3d43fa862510dfca to your computer and use it in GitHub Desktop.
Django get settings in template
import re
from django import template
from django.conf import settings
register = template.Library()
ALLOWABLE_VALUES = ("CONSTANT_NAME_1", "CONSTANT_NAME_2", "template_\w*")
@register.simple_tag
def settings_value(name):
"""Provide settings values to the templates.
Name of the value have to be in white list on this module called ALLOWABLE_VALUES.
Inspired by:
http://stackoverflow.com/a/21593607/2629036
http://stackoverflow.com/a/3040797/2629036
"""
regexes = '(?:%s)' % '|'.join(ALLOWABLE_VALUES)
if re.match(regexes, name):
return getattr(settings, name, '')
return ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment