I want to show my notification to all my templates. In my settings I have:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'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',
'helpers.views.notifications' ,
],
},
},
]
helpers/views.py
def notifications():
notifications = {'A':'aaa' , 'B':'bbb' }
return {'notifications': notifications }
I get nothing in my template - what am I doing wrong? in template :
{{notifications.A}}
Your TEMPLATES
setting looks ok. In your context processor, you don't need to use RequestContext
. Just return a dictionary. If you are using Django 1.9 or earlier, you must call the method request.user.is_authenticated()
otherwise request.user.is_authenticated
will always evaluate to True.
def notifications(request):
if request.user.is_authenticated(): # Use request.user.is_authenticated for Django >= 1.10
notifications = {'default: 'logged in', ... }
else:
notifications = {'default':'not logged in', ...}
return {'notifications': notifications }
Then in your tempalate, you can access {{ notifications.default }}