How to get logged in username in views.py in django
<p>Actually i'am very new to django and python. In /templates/home.html, added <strong>{{ user.username }}</strong> it's showing currently logged in username</p>

<pre><code>&lt;p&gt;Welcome {{ user.username }} !!!&lt;/p&gt;
</code></pre>

<p>I want to get currently logged in username in <strong>views.py</strong> file. How to get username?</p>

<p>i tried different way but i am not get the result</p>

<ol>
<li><code>user = User.objects.get(username=request.user.username)</code></li>
<li><code>username = request.GET['username']</code></li>
<li><p><code>def sample_view(request):
        current_user = request.user
        print(current_user)</code></p></li>
</ol>

<p>Please tell me, How to achieve my result.</p>

<p>my views.py look like this. is there any problem on my views.py</p>

<pre><code>    #!python
    #log/views.py
    from django.shortcuts import render
    from django.contrib.auth.decorators import login_required
    from django.template import Context
    from contextlib import contextmanager
    # Create your views here.
    # this login required decorator is to not allow to any
    # view without authenticating
    @login_required(login_url="login/")
    def home(request):
         return render(request,"home.html")
#dummy_user = {{ username }}
#user = User.objects.get(username=request.user.username)
#username = request.GET['username']
#print(usernam
#user = request.user
#print(user)

    def sample_view(request):
            current_user = {}
            #current_user['loggeduser'] = request.user
             #or
            current_user['loggeduser'] = request.user.username
            return render(request,"home.html",current_user)
           # print(current_user.id)
</code></pre>

<p>Provided that you have enabled the <a href="https://docs.djangoproject.com/en/1.10/ref/middleware/#django.contrib.auth.middleware.AuthenticationMiddleware" rel="nofollow">authentication middleware</a>, you don't need to do any of this. The fact that the username shows up in your template indicates that you have enabled it. Each view has access to a <code>request.user</code> that is an instance of a <code>User</code> model. So the following is very much redundant</p>

<pre><code>user = User.objects.get(username=request.user.username)
</code></pre>

<p>Because you already have <code>request.user.username</code>!! if you wanted to find the user's email, you do <code>request.user.email</code> Or just do</p>

<pre><code>user = request.user 
</code></pre>

<p>and use the newly created variable (eg user.username)</p>

<p>Reference: <a href="https://docs.djangoproject.com/en/1.10/ref/request-response/#django.http.HttpRequest.user" rel="nofollow">https://docs.djangoproject.com/en/1.10/ref/request-response/#django.http.HttpRequest.user</a></p>

<blockquote>
  <p>From the AuthenticationMiddleware: An instance of AUTH_USER_MODEL
  representing the currently logged-in user. If the user isnât currently
  logged in, user will be set to an instance of AnonymousUser. You can
  tell them apart with is_authenticated, like so:</p>
</blockquote>

python html django authentication oauth