ITeye博客

django-blog(四)自定义公共字段

xwins · 2016-11-02 22:00 · django · Web前端 · Markdown

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>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。