17π
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.
18π
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.
- [Django]-_() or {% trans %} in Django templates?
- [Django]-Authorization Credentials Stripped β django, elastic beanstalk, oauth
- [Django]-Accessing function as attribute in a Python class
2π
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
- [Django]-Django Query using .order_by() and .latest()
- [Django]-Raise a validation error in a model's save method in Django
- [Django]-How to write setup.py to include a Git repository as a dependency
1π
request.user refers to the actual user model instance.
request.user.FIELDNAME will allow you to access all the fields of the user model
- [Django]-Django β Simple custom template tag example
- [Django]-What is the difference between static files and media files in Django?
- [Django]-How do I test Django QuerySets are equal?