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
If you add a lot of custom `ModelAdmin` methods to `list_display` like I do, you know it can require a lot of repetition. Notice how adding 'checkbox' to `list_display` requires typing the method name 4 times:
class ExampleAdmin(admin.ModelAdmin):
list_display = ['checkbox', '__str__']
def checkbox(self, object):
return '<input type="checkbox" value="%s"/>' % object.pk
checkbox.short_description = mark_safe('✓')
checkbox.allow_tags = True
Using this decorator, the name only needs to be typed once:
class ExampleAdmin(admin.ModelAdmin):
list_display = ['__str__']
@add(list_display, mark_safe('✓'), 0, allow_tags=True)
def checkbox(self, object):
return '<input type="checkbox" value="%s"/>' % object.pk
- admin
- modeladmin
- list_display