设置全局变量

1、settings.py中配置

网站的基本信息配置

SITE_NAME = "XXXX的个人博客"
SITE_DESC = "以一滴水的平静,面对波澜不惊的人生"
WEIBO_SINA = 'http://weibo.com'
PRO_EMAIL = 'https://mail.qq.com'

2、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()

3、把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
...
]

4,可以直接在模板里调用名字

你可能感兴趣的:(设置全局变量)