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