Inter-page messaging in Django? (Django form-processing module tutorial and sessionMessage())


Just reading through the Django tutorial part four. One pattern from my own web-framework that didn't get covered is the "sessionMessage" tool. This is used when you submit a form via post, so you want to send the user to a new page, but you want to tell them what happened during the post (e.g. "Your order has been accepted").

The idea being that you can push messages via sessionMessage while working, then whether you render the page in the same request as the post (i.e. on an error condition) or via a redirect, your user gets the message. (Yes, there's the potential for messages showing up in a different browser window if you have some very-long-running requests, but it's never proven to be a problem, if they're switching that quickly they've still got the old page in mind when viewing the new page). In my site-wide templates there's code that displays the messages without any reference to what's being displayed, so there's no extra code required on an individual page to display the message. That lets you display messages from any page without worrying where the user will be when you're finished bouncing them around with redirects.

Ignoring how sessionMessage works in particular, I'd like to know whether there's a default mechanism for those kind of inter-page messages in Django (mostly because I had to write 2 or 3 ad-hoc ones earlier today, and that sucks).

Anyway, tired now. Work tomorrow.

Comments

  1. Simon Willison

    Simon Willison on 08/09/2005 12:16 p.m. #


    The good news is that Django has built in support for the exact pattern you describe. The bad news is that it only exists for logged-in users. Users in Django have an associated set of messages, along with a special "get_and_delete_messages" method for use by the code that displays any outstanding messages. It's not documented yet, but it's all in <a href="http://code.djangoproject.com/file/django/trunk/django/models/auth.py">this file</a>.<br />
    <br />
    How this will work when anonymous sessions are added to Django is currently up for discussion. Please bring it up on the mailing list!

Comments are closed.

Pingbacks

Pingbacks are closed.