码迷,mamicode.com
首页 > 其他好文 > 详细

django中间件templates写法

时间:2015-01-18 15:33:01      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

def templates_context_process(request):
    from django.contrib.sites.models import Site
    from accounts.models import ContactUsInfo
    if Site.objects.filter():
        name = Site.objects.filter()[0].name
    else:
        name = "example.com"
    contact = None
    if ContactUsInfo.objects.filter():
        contact = ContactUsInfo.objects.filter()[0]
    if contact:
        return {
            ‘site_name‘: name,
            ‘contact_QQ‘: contact.contact_QQ,
            ‘contact_phone‘: contact.contact_phone,
            ‘company_name‘: contact.company_name,
            ‘company_address‘: contact.company_address,
            ‘company_call‘: contact.company_call
        }
    else:
        return {
            ‘site_name‘: name,
        }

然后在settings里注册

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages",
    ‘helper.utils.templates_context_process‘
)

  

 

django中间件templates写法

标签:

原文地址:http://www.cnblogs.com/tuifeideyouran/p/4231752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!