1
Fork 0

Access (some of) django settings from templates

This commit is contained in:
Jake Howard 2016-11-24 18:03:37 +00:00
parent 6e00fd6095
commit f50d7d0a5e
6 changed files with 46 additions and 9 deletions

View file

19
project/common/context.py Normal file
View file

@ -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}

15
project/common/tests.py Normal file
View file

@ -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))

View file

@ -1,7 +1,5 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import models
from wagtail.wagtailcore.models import Page from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel from wagtail.wagtailadmin.edit_handlers import FieldPanel

View file

@ -20,9 +20,6 @@ EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
'project.home',
'project.search',
'wagtail.wagtailforms', 'wagtail.wagtailforms',
'wagtail.wagtailredirects', 'wagtail.wagtailredirects',
'wagtail.wagtailembeds', 'wagtail.wagtailembeds',
@ -46,6 +43,10 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'project.common',
'project.home',
'project.search',
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@ -80,7 +81,8 @@ TEMPLATES = [
'django.template.context_processors.request', 'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth', 'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages', '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 = [ STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'), os.path.join(BASE_DIR, 'static', 'build'),
] ]
STATIC_ROOT = os.path.join(BASE_DIR, 'collected-static') 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 - # 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 # e.g. in notification emails. Don't include '/admin' or a trailing slash
BASE_URL = 'http://example.com' BASE_URL = 'http://example.com'
SITE_URL = BASE_URL

View file

@ -9,9 +9,10 @@
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width" /> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width" />
<meta name="superfish" content="nofish" /> <meta name="superfish" content="nofish" />
<link rel="stylesheet" type="text/css" href="{% static 'css/project.css' %}"> <link rel="stylesheet" type="text/css" href="{% static 'css/index.css' %}">
{% block extra_css %}{% endblock %} {% block extra_css %}{% endblock %}
<script type="text/javascript" src="{% static 'js/jquery.js' %}"></script>
</head> </head>
<body class="{% block body_class %}{% endblock %}" id="page-top"> <body class="{% block body_class %}{% endblock %}" id="page-top">
@ -19,7 +20,8 @@
{% block content %}{% endblock %} {% block content %}{% endblock %}
<script type="text/javascript" src="{% static 'js/project.js' %}"></script> <script type="text/javascript" src="{% static 'js/libs.js' %}"></script>
<script type="text/javascript" src="{% static 'js/app.js' %}"></script>
{% block extra_js %}{% endblock %} {% block extra_js %}{% endblock %}
</body> </body>