django中间件templates写法
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' )