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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With