85

How can I get information about the logged-in user in a Django application?

For example:

I need to know the username of the logged-in user to say who posted a Review:

<form id='formulario' method='POST' action=''>
    <h2>Publica tu tuit, {{ usuario.username.title }} </h2>
    {% csrf_token %}
    {{formulario.as_p}}
    <p><input type='submit' value='Confirmar' /></p>
</form>

In usuario.username.title I get the username, but in the template, I need to get that information from the view.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Cris Towi
  • 1,141
  • 2
  • 10
  • 15

10 Answers10

124

You can use the request object to find the logged in user

def my_view(request):
    username = None
    if request.user.is_authenticated():
        username = request.user.username

According to https://docs.djangoproject.com/en/2.0/releases/1.10/

In version Django 2.0 the syntax has changed to

request.user.is_authenticated
Tyranno Taiwo
  • 190
  • 2
  • 9
karthikr
  • 97,368
  • 26
  • 197
  • 188
  • if request.user.is_authenticated(): TypeError: 'bool' object is not callable can you help on it django 3.2 – Mr Coder Aug 01 '21 at 09:11
48

request.user.get_username() or request.user.username, former is preferred.

Django docs say:

Since the User model can be swapped out, you should use this method instead of referencing the username attribute directly.

P.S. For templates, use {{ user.get_username }}

user
  • 17,781
  • 20
  • 98
  • 124
13

'request.user' has the logged in user.
'request.user.username' will return username of logged in user.

ancho
  • 1,060
  • 16
  • 24
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Sathish Aug 27 '14 at 09:59
  • it will return User object. you can get some user properties from this object. – ancho Dec 08 '15 at 04:42
12

For classed based views use self.request.user.id

Samsul Islam
  • 2,581
  • 2
  • 17
  • 23
ZongoBoyX
  • 121
  • 1
  • 5
6

You can use this to get the logged-in user's username :-

Just write this in template.

{{ request.user.username }}
Lars
  • 1,234
  • 1
  • 8
  • 31
3

if you are using the old way of writting views, in the way of Function-Based-Views...

in your view, you are creating a new variable called usuario to save the request.user probably...

but if you returning to the Template a context_instance, passing the value of the Context of the request, you will get the logged user, just by accessing the request.

// In your views file
from django.shortcuts import render_to_response
from django.template import RequestContext
def your_view(request):
    data = {
        'formulario': Formulario()
        # ...
    }
    return render_to_response('your_template.html',
        data, context_instance=RequestContext(request))


// In your template
<form id='formulario' method='POST' action=''>
    <h2>Publica tu tuit, {{ request.user.username.title }} </h2>
    {% csrf_token %}
    {{ formulario.as_p }}
    <p><input type='submit' value='Confirmar' /></p>
</form>
Luan Fonseca
  • 1,467
  • 1
  • 13
  • 22
  • To use this, you must have the `request template context processor` installed in your `settings.py`. [see this](http://stackoverflow.com/questions/13713077/get-user-information-in-django-templates) – ray6080 Mar 14 '14 at 13:20
  • Is 'Function-Based-Views...' an old way of writing views? – Gathide Jun 25 '22 at 04:49
  • Hi @Gathide, yea. If you see this answer is from 2013... back then the Class-based views weren't really a thing, nowadays the Request processor is pretty standard on templates and you can just use `{{ request.user.username }}`. I recommend you reading this: https://docs.djangoproject.com/en/4.0/topics/class-based-views/intro/ – Luan Fonseca Jun 29 '22 at 14:09
2

request.user.get_username() will return a string of the users email.

request.user.username will return a method.

Dan Walters
  • 1,218
  • 1
  • 18
  • 30
2

in this way you can check is user is logged in or not If yes for example go to profile if not back to login page

def profile(request):
    try:
        if request.user.username in User:
            return render(request, "profile.html", {'username': request.user.username})
    except:
        return redirect("/accounts/login")
0

You can use HttpRequest.user and get_user(request) to get the username of the logged-in user in Django Views as shown below:

# "views.py"

from django.shortcuts import render
from django.contrib.auth import get_user # Here

def test(request):
    print(request.user.username) # John
    print(get_user(request).username) # John
    return render(request, 'index.html')

And, you can get the username of the logged-in user in Django Templates as shown below:

{% "index.html" %}

{{ user }} {% John %}
{{ user.username }} {% John %}
{{ user.get_username }} {% John %}
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
-1

For template, you can use

{% firstof request.user.get_full_name request.user.username %}

firstof will return the first one if not null else the second one

Sehrish Waheed
  • 1,230
  • 14
  • 17