django 配置全局变量(模板中也可以使用)

1.写好返回全局变量的方法

def global_lang(request):
    return {
        'header_json':header_json
    }

2.在setting文件中添加配置项

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'), ],
        'APP_DIRS': True,
        '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',
                'XXX.XXX.global_lang'
            ],

        },

    },
]

3.配置之后 便可以在html模板中使用 header_json

{% include 'header.html' with header=header_json%}
或者
{{header_json}}

https://www.wj0511.com/site/detail.html?id=157

  

原文地址:https://www.cnblogs.com/changtao/p/12830577.html