from django.conf import settings
def misc(request):
return {'SITE_URL': settings.SITE_URL,'BALANCED_API_KEY':settings.BALANCED_API_KEY}`
在settings.py中我有import django.conf.global_settings as DEFAULT_SETTINGS
TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + ( os.path.join(BASE_DIR, 'cg1.cont_proc.misc'),)
BALANCED_API_KEY = os.environ.get('BALANCED_API_KEY')
SITE_URL = 'www.mysite.com' #but set up
>>> from django.conf import settings
>>> settings.TEMPLATE_CONTEXT_PROCESSORS
['django_balanced.context_processors.balanced_library','django_balanced.context_processors.balanced_settings', 'django.contrib.auth.context_processors.auth']
>>>>import os
>>>>os.environ.get('BALANCED_API_KEY')
'correct key from a local .env file'
我已经尝试了很多这样的问题,特别是:Where is template context processor in Django 1.5?
还:Python/Django is importing the wrong module (relative when it should be absolute)
但是django似乎没有在shell中看到我的自定义上下文处理器cont_proc。当我在视图中使用渲染时,我的模板不会收到变量。
答案 0 :(得分:1)
我安装了django-balanced。显然这是一个错误。我从已安装的应用中移除了一切都很好。