vim blogproject/settings.py
SITE_NAME='测试标题'
SITE_DESC='测试测试测试按时打卡机安检时刻'
[root@localhost blogproject]# cat blog/views.py
from django.shortcuts import render
import logging
from django.conf import settings
# Create your views here.
logger=logging.getLogger("blog.views")
def global_setting(request):
SITE_NAME=settings.SITE_NAME
SITE_DESC=settings.SITE_DESC
return locals()
def index(request):
try:
f = open('a.txt')
except Exception as e:
print e
logger.error(e)
return render(request,'index.html')
#vim blogproject/settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [ os.path.join(BASE_DIR,'templates')], #templates seting
'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_setting'
],
},
},
]
使用
[root@localhost blogproject]# cat templates/index.html
<div class="logo">
<h1>{{ SITE_NAME }}</h1>
<p>{{ SITE_DESC }}</p>
</div>