What does request user refer to in Django

0 votes

I have confusion regarding what does request.user refers to in Django? Does it refer to username field in the auth_user table or does it refer to User model instance?

I had this doubt because I was not able to access email field in the template using {{request.user.username}} or {{user.username}}.

So instead I did following in views file:

userr = User.objects.get(username=request.user)

And passed userr to the template and accessed email field as {{ userr.email }}.

Although its working but I wanted to have some clarity about it.

Jun 23, 2020 in Python by kartik
• 37,490 points
2,310 views

1 answer to this question.

0 votes

Hello @kartik,

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.

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

Hope it is helpful!!

Thank You!

answered Jun 23, 2020 by Niroj
• 82,560 points

Related Questions In Python

0 votes
1 answer

What to do when len() does not return the number of integers in range(1,100)?

You can use the following code block: x=range(1,100) len(x) Output: ...READ MORE

answered May 28, 2019 in Python by Fata
• 1,050 points
89 views
0 votes
1 answer

How to access the user profile in a Django template?

Hello @kartik, In your profile model provide related_name user ...READ MORE

answered Jul 29, 2020 in Python by Niroj
• 82,560 points
1,082 views
0 votes
1 answer

How to extend the User model with custom fields in Django?

Hello @kartik, Define an object manager for your ...READ MORE

answered Aug 3, 2020 in Python by Niroj
• 82,560 points
407 views
0 votes
1 answer

How to parse `request.body` from POST in Django?

Hello @kartik, In Python 3.0 to Python 3.5.x, json.loads() will ...READ MORE

answered Aug 5, 2020 in Python by Niroj
• 82,560 points
4,663 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,842 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,420 views
0 votes
1 answer

How to get Request.User in Django-Rest-Framework serializer?

Hello @kartik, You cannot access the request.user directly. You need ...READ MORE

answered Aug 12, 2020 in Python by Niroj
• 82,560 points
2,459 views
0 votes
1 answer

How to update user password in Django Rest Framework?

Hello @kartik, Using a modelserializer might be an ...READ MORE

answered Jul 1, 2020 in Python by Niroj
• 82,560 points
2,861 views