---
title: "django-blog（四）自定义公共字段"
author: "xwins"
date: "2016-11-02T22:00:51Z"
tags: []
categories: ["Web前端"]
slug: "django-blog"
draft: false
lastmod: "2016-11-02T22:00:51Z"
id: 2334983
url: "https://886.iteye.com/blog/2334983"
---

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 

  &lt;div class="logo"&gt;

      &lt;h1&gt;{{ SITE\_NAME }}&lt;/h1&gt;

      &lt;p&gt;{{ SITE\_DESC }}&lt;/p&gt;

  &lt;/div&gt;