美文网首页《Django By Example》Django知识与坑
django 在模板templates中引用全局变量

django 在模板templates中引用全局变量

作者: sun_wenming | 来源:发表于2018-06-13 11:27 被阅读1次

更多django知识点击访问

⑴ 在项目根目录下创建conf/global.py文件

#global.py
from django.conf import settings

def global_setting(request):   
    content = {
        'STATIC_URL': settings.STATIC_URL,   #把setting定义的读取出来
    }
    return content

⑵ 在setting.py中templates加入global_setting文件

#settting.py  
TEMPLATES = [
    { 'OPTIONS': {
            'context_processors': [
                'conf.global.global_setting',
            ], },},]

⑶ 之后就可以在templates下的html中愉快的使用了

#xx.html
<script src="{{STATIC_URL}}js/bootstrap.js"></script>

相关文章

网友评论

    本文标题:django 在模板templates中引用全局变量

    本文链接:https://www.haomeiwen.com/subject/becjeftx.html