Dynamic Django settings context processor

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# utils/context_processors.py

import sys

from django.conf import settings as django_settings

class SettingsProcessor(object):
    def __getattr__(self, attr):
        if attr == '__file__':
            # autoreload support in dev server
            return __file__
        else:
            return lambda request: {attr: getattr(django_settings, attr)}

sys.modules[__name__ + '.settings'] = SettingsProcessor()

# in settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
    # ....
    'utils.context_processors.settings.GOOGLEMAPS_KEY',
    'utils.context_processors.settings.TEMPLATE_DEBUG',
    'utils.context_processors.settings.MAXMIND_URL',
)

More like this

  1. Using Akismet/TypePad AntiSpam with Django's new comments framework by sciyoshi 3 years, 5 months ago
  2. Custom Django manager that excludes subclasses by sciyoshi 3 years, 6 months ago
  3. Prevent Django newcomments spam with Akismet (reloaded) by sciyoshi 2 years, 6 months ago
  4. EmailListField for Django by sciyoshi 2 years, 6 months ago
  5. Django ChoiceField with "other" choice by sciyoshi 3 years, 7 months ago

Comments

(Forgotten your password?)