django全局模板变量
views.py中:定义一个global_settings函数,先把全局变量都引入过来,然后再返回:
def global_setting(request):#注意有request参数
SITE_DESC = settings.SITE_DESC
SITE_NAME = settings.SITE_NAME
SITE_URL = settings.SITE_URL
return locals()
把global_setting函数作为一个处理器引入:
TEMPLATES = [
{
...
'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',
'blog.views.global_setting',#每个页面都会有global_setting
...
]
模板中调用,可以直接用名字了。