From f50d7d0a5eecdf1ed786ac431571c96459c45583 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 24 Nov 2016 18:03:37 +0000 Subject: [PATCH] Access (some of) django settings from templates --- project/common/__init__.py | 0 project/common/context.py | 19 +++++++++++++++++++ project/common/tests.py | 15 +++++++++++++++ project/home/models.py | 2 -- project/settings.py | 13 ++++++++----- templates/base.html | 6 ++++-- 6 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 project/common/__init__.py create mode 100644 project/common/context.py create mode 100644 project/common/tests.py diff --git a/project/common/__init__.py b/project/common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/project/common/context.py b/project/common/context.py new file mode 100644 index 0000000..5189299 --- /dev/null +++ b/project/common/context.py @@ -0,0 +1,19 @@ +from django.conf import settings + + +SETTINGS_KEYS = [ + 'SITE_URL', + 'BASE_URL', + 'STATIC_URL', + 'MEDIA_URL', + 'LANGUAGE_CODE', + 'TIME_ZONE', + 'ALLOWED_HOSTS' +] + + +def settings_injector(request=None): + injected_settings = {} + for setting in SETTINGS_KEYS: + injected_settings[setting] = getattr(settings, setting) + return {'django_settings': injected_settings} diff --git a/project/common/tests.py b/project/common/tests.py new file mode 100644 index 0000000..6a23357 --- /dev/null +++ b/project/common/tests.py @@ -0,0 +1,15 @@ +from wagtail.tests.utils import WagtailPageTests +from .context import SETTINGS_KEYS +from django.conf import settings + + +class BaseTestCase(WagtailPageTests): + pass + + +class ContextInjectorTestCase(BaseTestCase): + def test_has_keys(self): + response = self.client.get('/') + for key in SETTINGS_KEYS: + self.assertIn(key, response.context['django_settings']) + self.assertEqual(response.context['django_settings'][key], getattr(settings, key)) diff --git a/project/home/models.py b/project/home/models.py index 9d8415c..08a5aa2 100755 --- a/project/home/models.py +++ b/project/home/models.py @@ -1,7 +1,5 @@ from __future__ import unicode_literals -from django.db import models - from wagtail.wagtailcore.models import Page from wagtail.wagtailcore.fields import RichTextField from wagtail.wagtailadmin.edit_handlers import FieldPanel diff --git a/project/settings.py b/project/settings.py index 10854ba..5e1d6a2 100755 --- a/project/settings.py +++ b/project/settings.py @@ -20,9 +20,6 @@ EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' # Application definition INSTALLED_APPS = [ - 'project.home', - 'project.search', - 'wagtail.wagtailforms', 'wagtail.wagtailredirects', 'wagtail.wagtailembeds', @@ -46,6 +43,10 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + + 'project.common', + 'project.home', + 'project.search', ] MIDDLEWARE = [ @@ -80,7 +81,8 @@ TEMPLATES = [ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', - 'wagtail.contrib.settings.context_processors.settings' + 'wagtail.contrib.settings.context_processors.settings', + 'project.common.context.settings_injector' ], }, }, @@ -115,7 +117,7 @@ USE_TZ = True STATICFILES_DIRS = [ - os.path.join(BASE_DIR, 'static'), + os.path.join(BASE_DIR, 'static', 'build'), ] STATIC_ROOT = os.path.join(BASE_DIR, 'collected-static') @@ -140,3 +142,4 @@ WAGTAILSEARCH_BACKENDS = { # 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' +SITE_URL = BASE_URL diff --git a/templates/base.html b/templates/base.html index 4bb751e..72e08c1 100755 --- a/templates/base.html +++ b/templates/base.html @@ -9,9 +9,10 @@ - + {% block extra_css %}{% endblock %} + @@ -19,7 +20,8 @@ {% block content %}{% endblock %} - + + {% block extra_js %}{% endblock %}