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

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM