Login

Snippets by alexk

Snippet List

Cookie based flash errors and notices (a la Rails)

This is a light-weight flash implementation. Instead of hitting the database it uses cookies. The messages are shown to the user only once, after that the cookies are deleted. I tested it on Google App Engine, but it should work on vanilla Django as well, there's no GAE specific code. To set up, add `"path.to.flash.Middleware"` to the `MIDDLEWARE_CLASSES` list. Also add `'path.to.flash.context_processor'` to the `TEMPLATE_CONTEXT_PROCESSORS` list. In your views, import and call `flash_error(msg)` and `flash_notice(msg)` passing the message that you want to show. In your base template use this mark up: {% if flash.notice %} <div id="flash_notice"> <p>{{ flash.notice }}</p> </div> {% endif %} {% if flash.error %} <div id="flash_error"> <p>{{ flash.error }}</p> </div> {% endif %} And finally, add this to your CSS file changing colours as necessary: #flash_error { margin-top: 30px; padding: 20px; background-color: #FFCCCC; border: solid 1px #CC0000; } #flash_notice { margin-top: 30px; padding: 20px; background-color: #CCFFCC; border: solid 1px #00CC00; } #flash_error p, #flash_notice p { margin: 0px; } Please comment if you notice any FUs. I'm new to Django and will appreciate any feedback.

  • error
  • flash
  • rails
  • notification
  • notice
Read More

alexk has posted 1 snippet.