title: How to get logged in username in views.py in django
body: <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>