Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does request.user refer to in Django?

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.

like image 978
zephyr Avatar asked Jun 26 '13 06:06

zephyr


People also ask

What is request get in Django?

In this Django tutorial, you will learn how to get data from get request in Django. When you send a request to the server, you can also send some parameters. Generally, we use a GET request to get some data from the server. We can send parameters with the request to get some specific data.

How does Django handle request?

Whenever a request comes into Django, it is handled by middlewares. When the Django server starts, the first thing it loads after settings.py is middlewares. The Request is processed by various middlewares one at a time. So, from the above list, when the request comes it will be passed through the security middleware.

What is request POST in Django?

request. POST is an attribute of this request object, it's a QueryDict (much similar to a normal Python dict). It contains the HTTP POST parameters that are sent to your view.

What does {{ NAME }} this mean in Django templates?

What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.


1 Answers

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.

like image 59
falsetru Avatar answered Oct 14 '22 16:10

falsetru