Access (some of) django settings from templates
This commit is contained in:
parent
6e00fd6095
commit
f50d7d0a5e
6 changed files with 46 additions and 9 deletions
0
project/common/__init__.py
Normal file
0
project/common/__init__.py
Normal file
19
project/common/context.py
Normal file
19
project/common/context.py
Normal 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
15
project/common/tests.py
Normal 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))
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -9,9 +9,10 @@
|
|||
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width" />
|
||||
<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 %}
|
||||
<script type="text/javascript" src="{% static 'js/jquery.js' %}"></script>
|
||||
</head>
|
||||
|
||||
<body class="{% block body_class %}{% endblock %}" id="page-top">
|
||||
|
@ -19,7 +20,8 @@
|
|||
|
||||
{% 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 %}
|
||||
</body>
|
||||
|
|
Reference in a new issue