class SessionMessageContainer: """ In a view create messages with: SessionMessageContainer.append_to_session(request.session, u"Order with id: %s saved"%order.id) Output in template with:
""" def append(self, message): self.messages.append(message) def get_and_clear(self): from django.contrib.sessions.backends.db import SessionStore messages = self.messages self.messages=[] s = SessionStore(session_key=self.key) s['messages']=self s.save() return messages def __init__(self, key): self.key = key self.messages = [] @staticmethod def append_to_session(session, message): messages = session.get('messages', SessionMessageContainer(key=session.session_key)) messages.append(message) session['messages']=messages