Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Django, is it possible to access the current user session from within a custom tag?

Tags:

I am writing a custom tag in Django that should output a value stored in a user session, but I cannot find a way to access the session object from within a custom tag function. Is there any way to do this, without manually assigning the session object to a context variable?

like image 538
knabar Avatar asked Dec 02 '08 20:12

knabar


People also ask

How do I know who is online in Django?

But the correct way to check if a user is logged in or not is to use : request. user. is_authenticated. This will return True if the person is logged in other wise False.


1 Answers

You should be able to add the request context processor in your settings.py file:

TEMPLATE_CONTEXT_PROCESSORS = ("django.core.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", 'django.core.context_processors.request',) 

This will do the same thing as the current answer, without having to add a custom file.

like image 147
Michael Warkentin Avatar answered Sep 20 '22 19:09

Michael Warkentin