title: Custom LoginView in Django 2
body: <p>I am trying to customise the authentication and view in Django 2 but the problem is that if the user is already authenticated the login form is still shown and it is not redirected to the appropriate URL. To get over this I have done the following:</p>
  
  <pre><code>class CustomLoginView(LoginView):
  
      form_class = LoginForm
      template_name = 'login.html'
  
      def get_initial(self):
          if self.request.user.is_authenticated and self.request.user.is_staff and has_2fa(self.request.user):
              return HttpResponseRedirect(reverse('{}'.format(self.request.GET.get('next', 'portal_home'))))
          else:
              return self.initial.copy()
  
      def form_valid(self, form):
  
          if self.request.user.is_staff and not has_2fa(self.request.user):
              logger.info('is staff but does not have 2FA, redirecting to Authy account creator')
              auth_login(self.request, form.get_user())
              return redirect('2fa_register')
          auth_login(self.request, form.get_user())
  
          return HttpResponseRedirect(self.get_success_url())
  </code></pre>
  
  <p>But the <code>HttpResponseRedirect</code> in <code>get_initial()</code> does not redirect to the <code>/portal/</code> page. I have also tried <code>redirect('portal_home')</code> but nothing happens or do I need to write a custom <code>dispatch</code>?</p>
  
  <p>Any help would be much appreciated.</p>