我想用一個模板來加載站點名稱:訪問在setting.py變量從模板使用Django 1.4
{{ SITE_NAME }}
在setting.py
我:
SITE_NAME = "MySite"
和
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP
TEMPLATE_CONTEXT_PROCESSORS = TCP + (
'django.core.context_processors.request',
)
我也使用基於類的視圖來加載我的視圖(views.py
):
from django.views.generic import TemplateView
class MenuNavMixin(object):
def get_context_data(self, **kwargs):
context = super(MenuNavMixin, self).get_context_data(**kwargs)
return context
class AboutView(MenuNavMixin, TemplateView):
template_name = "home/about.html"
urls.py
:
url(r'^about/$', AboutView.as_view(), name='about'),
我不能home/about.html
訪問SITE_NAME
除非我明確它添加到上下文變量:
import mywebsite.settings
class MenuNavMixin(object):
def get_context_data(self, **kwargs):
context = super(MenuNavMixin, self).get_context_data(**kwargs)
context['SITE_NAME'] = mywebsite.settings.SITE_NAME
return context
我認爲這是不如果我使用的情況:
TEMPLATE_CONTEXT_PROCESSORS = TCP + (
'django.core.context_processors.request',
)
任何人都可以指向正確的方向嗎?
我以前的答案:http://stackoverflow.com/questions/433162/can-i-access-constants-in-settings-py-from-templates-in-django/7716141#7716141 –