Created
January 5, 2017 21:29
-
-
Save fdobrovolny/632f56ddb907108b3d43fa862510dfca to your computer and use it in GitHub Desktop.
Django get settings in template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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