[Django]-What does request.user refer to in Django?

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.

πŸ‘€emigue

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.

πŸ‘€falsetru

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

πŸ‘€suhailvs

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

πŸ‘€jammas615

Leave a comment