[Django]-Django display context data in template without calling view through url

1👍

It sounds like you want to add certain variables to the context of every view, rather than just this one.

One way to do it is using a context processor:

# myapp/context_processors.py

def notifications(request):
    "Context processor for adding notifications to the context."
    return {
        'unseen_notifications': Notification.objects.filter(
            body__user=request.user).filter(viewed=False),
        'seen_notifications': Notification.objects.filter(
            body__user=request.user).filter(viewed=True),
    }

You’d also need to add the context processor to TEMPLATE_CONTEXT_PROCESSORS:

# settings.py

...
TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'myapp.context_processors.notifications',
)

1👍

I think it’s impossible to include template view via “include” template tag. Include loads template in current context https://docs.djangoproject.com/en/dev/ref/templates/builtins/#include.

It seems to me that you should use custom template tag for your purpose.
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

Leave a comment