Ticket #4604: visitor_messages.patch
File visitor_messages.patch, 4.9 KB (added by , 17 years ago) |
---|
-
django/contrib/sessions/context_processors.py
1 def visitor_messages(request): 2 """ 3 Returns any messages in the session's message queue. 4 """ 5 return { 6 'visitor_messages': request.session.get_and_delete_messages(), 7 } -
django/contrib/sessions/middleware.py
6 6 7 7 TEST_COOKIE_NAME = 'testcookie' 8 8 TEST_COOKIE_VALUE = 'worked' 9 MESSAGES_NAME = '_messages' 9 10 10 11 class SessionWrapper(object): 11 12 def __init__(self, session_key): … … 48 49 def delete_test_cookie(self): 49 50 del self[TEST_COOKIE_NAME] 50 51 52 def get_messages(self): 53 return self.get(MESSAGES_NAME, []) 54 55 def get_and_delete_messages(self): 56 return self.pop(MESSAGES_NAME, []) 57 58 def create_message(self, message): 59 messages = self.get(MESSAGES_NAME) 60 if messages is None: 61 messages = [] 62 self[MESSAGES_NAME] = messages 63 messages.append(message) 64 self.modified = True 65 51 66 def _get_session(self): 52 67 # Lazily loads session from storage. 53 68 self.accessed = True -
django/contrib/sessions/tests.py
10 10 11 11 >>> s.pop('some key', 'does not exist') 12 12 'does not exist' 13 14 ############ 15 # Messages # 16 ############ 17 18 >>> s.get_messages() 19 [] 20 21 >>> s.create_message('first post') 22 >>> s.get_messages() 23 ['first post'] 24 >>> s.get_and_delete_messages() 25 ['first post'] 26 >>> s.get_and_delete_messages() 27 [] 28 29 >>> s.create_message('hello') 30 >>> s.create_message('world') 31 >>> s.get_and_delete_messages() 32 ['hello', 'world'] 13 33 """ 14 34 15 35 from django.contrib.sessions.middleware import SessionWrapper -
docs/authentication.txt
911 911 Finally, note that this messages framework only works with users in the user 912 912 database. To send messages to anonymous users, use the `session framework`_. 913 913 914 .. _session framework: ../sessions/ 914 .. _session framework: ../sessions/#visitor-messages 915 915 916 916 Other authentication sources 917 917 ============================ -
docs/sessions.txt
150 150 request.session.set_test_cookie() 151 151 return render_to_response('foo/login_form.html') 152 152 153 Visitor Messages 154 ================ 155 156 **New in Django development version** 157 158 The session message system provides a simple way to queue messages for 159 anonymous site visitors. To associate messages with users in the user database, 160 use the `authentication message framework`_. 161 162 .. _authentication message framework: ../authentication/#messages 163 164 Messages are associated with a session, therefore a message only lasts as long 165 as a session is valid (see `browser-length sessions vs. persistent sessions`_). 166 167 The message system relies on the session middleware and is accessed via 168 ``request.session``. The API is simple: 169 170 * To create a new message, use 171 ``request.session.create_message(message='message text').`` 172 173 * To retreive the messages, use ``request.session.get_messages()``, 174 which returns a list of any messages (strings) in the session's queue. 175 176 * To retrieve and delete messages, use 177 ``user_obj.get_and_delete_messages()``, which returns the list of any 178 messages in the session's queue and then deletes the messages from the 179 queue. 180 181 django.contrib.sessions.context_processors.visitor_messages 182 ----------------------------------------------------------- 183 184 This `context processor`_ is provided (but not installed by default) which makes 185 these messages available to the template context as the template variable 186 ``{{ visitor_messages }}`` when you use ``RequestContext``. 187 188 .. _context processor: ../templates_python/#subclassing-context-requestcontext 189 190 Here's an example of template code that displays messages:: 191 192 {% if visitor_messages %} 193 <ul> 194 {% for message in visitor_messages %} 195 <li>{{ message|escape }}</li> 196 {% endfor %} 197 </ul> 198 {% endif %} 199 200 Note that when this context processor is accessed by ``RequestContext``, 201 ``get_and_delete_messages`` is called behind the scenes, so any messages will 202 be deleted even if you don't display them. 203 153 204 Using sessions out of views 154 205 =========================== 155 206