From 53cbee964f5b4b00112e8aafedeff537f8f35982 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 24 Jul 2018 21:16:55 +0100 Subject: [PATCH] Allow access to settings in templates --- project/common/context.py | 18 +++++++++++++ project/common/models.py | 2 +- project/settings.py | 50 ++++++++++++++++++++--------------- templates/home/home_page.html | 1 + 4 files changed, 49 insertions(+), 22 deletions(-) create mode 100644 project/common/context.py diff --git a/project/common/context.py b/project/common/context.py new file mode 100644 index 0000000..c195715 --- /dev/null +++ b/project/common/context.py @@ -0,0 +1,18 @@ +from django.conf import settings + + +SETTINGS_KEYS = [ + 'BASE_URL', + 'STATIC_URL', + 'MEDIA_URL', + 'LANGUAGE_CODE', + 'TIME_ZONE', + 'ALLOWED_HOSTS', + 'WAGTAIL_SITE_NAME' +] + + +def settings_injector(request): + return {'settings': { + setting: getattr(settings, setting) for setting in SETTINGS_KEYS + }} diff --git a/project/common/models.py b/project/common/models.py index 459b508..9427bef 100644 --- a/project/common/models.py +++ b/project/common/models.py @@ -27,7 +27,7 @@ class Entity(MetadataPageMixin, Page): return self.search_image def get_meta_description(self): - return self.search_description or self.short_body + return self.search_description class Meta: abstract = True diff --git a/project/settings.py b/project/settings.py index b11add6..29e4c1a 100644 --- a/project/settings.py +++ b/project/settings.py @@ -17,6 +17,22 @@ import django_cache_url BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +WAGTAIL_SITE_NAME = "project" + +# Base URL to use when referring to full URLs within the Wagtail admin backend - +# e.g. in notification emails. Don't include '/admin' or a trailing slash +BASE_URL = 'http://example.com' + +DEBUG = True + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = '@%p6v$-d_+)v!=5ow9-*!lw@2n+&9!f83^+p)#jegc%7h122xe' + +# SECURITY WARNING: define the correct hosts in production! +ALLOWED_HOSTS = ['*'] + +EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' + # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ @@ -77,23 +93,30 @@ TEMPLATES = [ 'DIRS': [ os.path.join(BASE_DIR, 'templates'), ], + 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', - ], - 'loaders': [ - ('django.template.loaders.cached.Loader', [ - 'django.template.loaders.filesystem.Loader', - 'django.template.loaders.app_directories.Loader', - ]), + 'project.common.context.settings_injector' ], }, }, ] +if not DEBUG: + TEMPLATES[0]['OPTIONS']['loaders'] = [ + ('django.template.loaders.cached.Loader', [ + 'django.template.loaders.filesystem.Loader', + 'django.template.loaders.app_directories.Loader', + ]), + ] + del TEMPLATES[0]['APP_DIRS'] + + + WSGI_APPLICATION = 'project.wsgi.application' @@ -158,21 +181,6 @@ MEDIA_URL = '/media/' # Wagtail settings -WAGTAIL_SITE_NAME = "project" - -# Base URL to use when referring to full URLs within the Wagtail admin backend - -# e.g. in notification emails. Don't include '/admin' or a trailing slash -BASE_URL = 'http://example.com' - -DEBUG = True - -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = '@%p6v$-d_+)v!=5ow9-*!lw@2n+&9!f83^+p)#jegc%7h122xe' - -# SECURITY WARNING: define the correct hosts in production! -ALLOWED_HOSTS = ['*'] - -EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' if 'CACHE_URL' in os.environ: CACHES = {'default': django_cache_url.config()} diff --git a/templates/home/home_page.html b/templates/home/home_page.html index 954c6f4..8a50e32 100644 --- a/templates/home/home_page.html +++ b/templates/home/home_page.html @@ -2,4 +2,5 @@ {% block content %} {{ self.body }} + {{ settings.WAGTAIL_SITE_NAME }} {% endblock %}