title: How to prevent going to login page while loggedin in django
body: <p>As the title explains, although I'm successfully logged in, I can't prevent the application from going back to the login page if I entered the path to the page in URL bar.
  NOTE:I'm not using the built-in user or authentication classes.
  Here is the code below:</p>
  
  <pre><code>class user_login_view(View):
      form_class = LoginForm
      template_name = 'main/login.html'
  
      def get(self, request):
          form = self.form_class(None)
          return render(request, self.template_name, {'form': form})
  
      def post(self, request):
          form = self.form_class(request.POST)
          username = request.POST['username']
          password = request.POST['password']
          if form.is_valid:
              try:
                  member = user.objects.filter(username=username).first()
              except user.DoesNotExist:
                  member = None
              if member != None and member and member.password == password:
                  request.session['username'] = username
                  return redirect('main:index')
              else:
                  messages.error(request,'account does not exist')
                  return render(request, self.template_name,{'form': form})
          else:
              messages.error(request, 'account does not exist')
              return render(request, self.template_name,{'form': form})
  
  def index(request):
      template_name = 'main/loggedin.html'
      if request.session.has_key('username'):
          username = request.session['username']
          return render(request, template_name, {"username" : username})
      else:
          return HttpResponseRedirect(reverse('main:login'))
  
  def logout(request):
      try:
          del request.session['username']
      except:
          pass
      return HttpResponseRedirect(reverse('main:login'))
  </code></pre>