What does request.user refer to in Django?
If your template is receiving AnonymousUser, the reference to {{request.user.email}}
will not be found. Previously, you must ask if {{request.user.is_authenticated }}
.
You must check if it is included django.core.context_processors.auth
context processor in TEMPLATE_CONTEXT_PROCESSORS
section of settings. If you are using Django 1.4 or latest, then context processor is django.contrib.auth.context_processors.auth
. This context processor is responsible to include user object in every request.
It depends upon what you set .
So, it is better to use
user = User.objects.get(username=request.user.username)
Actually, you don't need to define such variables if you append 'django.core.context_processors.request'
into the TEMPLATE_CONTEXT_PROCESSORS
list in settings.py
Then you can access the variable {{ request.user.username }} in templates if you are using render
in views.py
request.user
is User model object.
You cannot access request object in template if you do not pass request
explicitly.
If you want access user object from template, you should pass it to template or use RequestContext.