博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django中的setting全局变量的导入
阅读量:7014 次
发布时间:2019-06-28

本文共 1583 字,大约阅读时间需要 5 分钟。

正确使用例子

settings.py

SITE_NAME = “站点名称”SITE_DESC = "站点描述"

views.py

from django.shortcuts import renderfrom django.conf import settingsdef global_settings(request):    return {        'SITE_NAME': settings.SITE_NAME,        'SITE_DESC': settings.SITE_DESC    }def index(request):    return render(request, 'index.html', locals())

settings.py

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',                'blog.views.global_settings'            ],        },    },]

index.html

{
{ SITE_NAME }}

{
{ SITE_DESC }}

为什么不能用import settings

import settings will import the first python module named settings.py found in sys.path, usually (in default django setups). It allows access only to your site defined settings file, which overwrites django default settings (django.conf.global_settings).

So, if you try to access a valid django setting not specified in your settings file you will get an error.

django.conf.settings is not a file but a class making an abstraction of the concepts, default settings and your site-specific settings. Django also does other checks when you use from django.conf import settings.

from django.conf import settings 是更好的选择

转载地址:http://uuqtl.baihongyu.com/

你可能感兴趣的文章
Checkly如何借助Terraform实现零宕机部署
查看>>
为什么已有Elasticsearch,我们还要重造实时分析引擎AresDB?
查看>>
玩大了,开源协议修改引发MongoDB“大动荡”?
查看>>
Kafka团队修改KSQL开源许可,怒怼云厂商
查看>>
腾讯云视频技术全面升级 明眸、Tencent-RTC首度亮相
查看>>
Elasticsearch 7.0中引入的新集群协调子系统如何使用?
查看>>
PostgreSQL中的大容量空间探索时间序列数据存储
查看>>
IBM借QISKit打造基于云平台的量子计算
查看>>
红帽发布 Ansible Tower 3.4:在混合云中实践DevOps更便捷
查看>>
你真的了解前端模块化吗?
查看>>
input type="search" 实现搜索框
查看>>
用PVS在.NET内核中发现的缺陷
查看>>
扎克伯格发信表示押注区块链,即时通讯 + 加密货币 = 全球化使用!
查看>>
高效 Mac 人士必备:实现工作/家庭间网络环境切换的自动化
查看>>
《Java 20年:道路与梦想》迷你书发布
查看>>
BitBucket引入灾难恢复和合并策略
查看>>
有赞透明多级缓存解决方案(TMC)设计思路
查看>>
PHP|一段Code
查看>>
ngModel使用说明小demo
查看>>
Combination Sum
查看>>