diff --git a/django/contrib/sessions/backends/base.py b/django/contrib/sessions/backends/base.py index acade4a512..40c557ba0b 100644 --- a/django/contrib/sessions/backends/base.py +++ b/django/contrib/sessions/backends/base.py @@ -306,7 +306,10 @@ class SessionBase(object): """ Creates a new session key, while retaining the current session data. """ - data = self._session_cache + try: + data = self._session_cache + except AttributeError: + data = {} key = self.session_key self.create() self._session_cache = data diff --git a/tests/sessions_tests/tests.py b/tests/sessions_tests/tests.py index 480eb72c8f..ffc7e9d942 100644 --- a/tests/sessions_tests/tests.py +++ b/tests/sessions_tests/tests.py @@ -188,6 +188,10 @@ class SessionTestsMixin(object): self.assertNotEqual(self.session.session_key, prev_key) self.assertEqual(list(self.session.items()), prev_data) + def test_cycle_with_no_session_cache(self): + self.assertFalse(hasattr(self.session, '_session_cache')) + self.session.cycle_key() + def test_save_doesnt_clear_data(self): self.session['a'] = 'b' self.session.save()