1
0
mirror of https://github.com/django/django.git synced 2025-08-23 10:19:13 +00:00

[1.7.x] Refs #21357 -- Added a working session example to the docs.

This commit is contained in:
Simon Baechler 2015-05-05 15:51:31 +02:00 committed by Tim Graham
parent 285371bd76
commit d42cbde0c6

View File

@ -475,14 +475,22 @@ can access these properties as part of a test condition.
A dictionary-like object containing session information. See the A dictionary-like object containing session information. See the
:doc:`session documentation</topics/http/sessions>` for full details. :doc:`session documentation</topics/http/sessions>` for full details.
To modify the session and then save it, it must be stored in a variable In Django 1.7, ``client.session`` returns a plain dictionary if the session
first (because a new ``SessionStore`` is created every time this property is empty. The following code creates a test client with a fully working
is accessed):: session engine::
def test_something(self): from importlib import import_module
session = self.client.session
session['somekey'] = 'test' from django.conf import settings
session.save() from django.test import Client
def get_client_with_session(self):
client = Client()
engine = import_module(settings.SESSION_ENGINE)
s = engine.SessionStore()
s.save()
client.cookies[settings.SESSION_COOKIE_NAME] = s.session_key
return client
Example Example
~~~~~~~ ~~~~~~~