title: Django - get URL from TemplateResponse
body: <p>I have a Django app that uses the builtin login system.</p>
  
  <p>I'm trying to write a unit test that verifies that the <code>login</code> url redirects to <code>home</code> if the user is already authenticated.  Meaning, if an already authenticated user tries to visit the <code>login</code> url, they get redirected to <code>home</code>.</p>
  
  <p>This is a built-in feature of Django's <code>LoginView</code>, and I have verified that it is indeed working.  However, I'm not sure how to write a unit test for this.</p>
  
  <p><strong>Question:</strong> How do I verify that this redirect occurs via unit tests?  <code>LoginView</code> returns a <code>TemplateResponse</code>, which doesn't have a <code>url</code> parameter.</p>
  
  <p>What I have so far is this:</p>
  
  <pre><code>from django.test import TestCase
  from django.urls import reverse
  
  home  = reverse('home')
  login = reverse('login')
  logout = reverse('logout')
  signup = reverse('signup')
  
  USER_CREDS = { some users credentials }
  
  class TestLoginView(TestCase):
      def test_login_redirect(self):
          self.client.post(signup, USER_CREDS)
          self.client.get(logout)
          self.client.post(login, USER_CREDS)
          response = self.client.get(login)
          self.assertRedirects(response, home)
  </code></pre>
  
  <p><strong>EDIT:</strong>
  <code>SimpleTestCase.assertRedirects</code> does not work for <code>TemplateResponse</code> objects.  </p>
  
  <p><code>TemplateResponse</code> objects do not contain a <code>url</code> parameter, which is required for <code>SimpleTestCase.assertRedirect</code>.</p>
  
  <p>The method raises the following error: <code>AttributeError: 'TemplateResponse' object has no attribute 'url'</code></p>
  
  <p>This is not the same as the linked question, because the response object from a <code>LoginView</code> is a <code>TemplateResponse</code>, not an <code>HttpResponse</code>.</p>