title: Getting username of logged in user in django
body: <p>I am new to Django and I just want to find a way to get the username of a logged in user and use it to get the boards of the user. I may have done the models wrong by adding another user model but I would like some help in getting the user's username. Here is my views file:</p>
  
  <pre><code>    from django.shortcuts import render
  from django.urls import reverse
  from django.http import HttpResponseRedirect
  from django.contrib.auth import authenticate,login
  from django.contrib.auth.models import User
  from .models import board_member,user
  # Create your views here.
  
  def index(request):
  # Login form submitted?
      if request.method == 'POST':
          username = request.POST.get('username')
          password = request.POST.get('password')
          if username and password:
              user = authenticate(username=username, password=password)
              # Login succeeded
              if user is not None:
                  login(request, user)
                  return HttpResponseRedirect(reverse('login_success'))
          # Login failed
          return HttpResponseRedirect(reverse('login_fail'))
      return render(request, 'Boards/login.html')
  
  def login_success(request):
      return render(request, 'Boards/Homepage.html')
  
  def getboards(request):
      username = request.user.get_username()
      boards = []
      for Memberships in board_member.objects.all():
          if username == Memberships.user.username:
              boards.append(Memberships.board)
      return render(request, 'Boards/Homepage.html', {'boards': boards, 'username':username})
  
  def login_fail(request):
      return render(request, 'login_fail.html')
  </code></pre>
  
  <p>and this is my models file:</p>
  
  <pre><code>from django.db import models
  from django.contrib.auth.models import User
  from django.db.models.signals import post_save
  from django.dispatch import receiver
  
  class board(models.Model):
      name = models.CharField(max_length=20)
  
      def __str__(self):
          return (self.name)
  
  class user(models.Model):
      username = models.CharField(max_length=20)
      password = models.CharField(max_length=20)
      boards = models.ManyToManyField(
          board, through='board_member', related_name="members")
  
      def __str__(self):
          return(self.username)
  
  
  class list(models.Model):
      title = models.CharField(max_length=20)
      board = models.ForeignKey(
          board, related_name="lists", on_delete=models.CASCADE)
  
  class board_member(models.Model):
      user = models.ForeignKey(
          user, related_name="memberships", on_delete=models.CASCADE)
      board = models.ForeignKey(
          board, related_name="memberships", on_delete=models.CASCADE)
      access = models.CharField(max_length=20)
  
      def __str__(self):
          return ("%s is %s in board %s" % (self.user, self.access, self.board))
  
  
  class card(models.Model):
      list = models.ForeignKey(list, related_name="cards",
                               on_delete=models.CASCADE)
      text = models.CharField(max_length=100)
      date = models.DateField(auto_now=True)
      due = models.DateField()
      flag = models.CharField(max_length=20)
  </code></pre>