diff --git a/django/contrib/auth/views.py b/django/contrib/auth/views.py index be156286c6..cdb6dded93 100644 --- a/django/contrib/auth/views.py +++ b/django/contrib/auth/views.py @@ -107,6 +107,11 @@ class LoginView(SuccessURLAllowedHostsMixin, FormView): def get_form_class(self): return self.authentication_form or self.form_class + def get_form_kwargs(self): + kwargs = super(LoginView, self).get_form_kwargs() + kwargs['request'] = self.request + return kwargs + def form_valid(self, form): """Security check complete. Log the user in.""" auth_login(self.request, form.get_user()) diff --git a/tests/auth_tests/test_views.py b/tests/auth_tests/test_views.py index 2d0d2ae96f..5a6fc81610 100644 --- a/tests/auth_tests/test_views.py +++ b/tests/auth_tests/test_views.py @@ -598,13 +598,14 @@ class LoginTest(AuthViewsTestCase): self.assertEqual(response.url, settings.LOGIN_REDIRECT_URL) def test_login_form_contains_request(self): - # 15198 - self.client.post('/custom_requestauth_login/', { + # The custom authentication form for this login requires a request to + # initialize it. + response = self.client.post('/custom_request_auth_login/', { 'username': 'testclient', 'password': 'password', - }, follow=True) - # the custom authentication form used by this login asserts - # that a request is passed to the form successfully. + }) + # The login was successful. + self.assertRedirects(response, settings.LOGIN_REDIRECT_URL, fetch_redirect_response=False) def test_login_csrf_rotate(self): """