tg.flash
– Flash¶Flash messaging system for sending info to the user in a non-obtrusive way
tg.flash.
TGFlash
(**options)¶Support for flash messages stored in a plain cookie.
Supports both fetching flash messages on server side and on client side through Javascript.
When used from Python itself, the flash object provides a TGFlash.render()
method that can be used from templates to render the flash message.
When used on Javascript, calling the TGFlash.render()
provides a webflash
javascript object which exposes .payload()
and .render()
methods that can
be used to get current message and render it from javascript.
For a complete list of options supported by Flash objects see TGFlash.configure()
.
configure
(cookie_name='webflash', default_status='ok', template=<string.Template object>, js_call='webflash.render()', js_template=<string.Template object>, allow_html=False)¶Flash messages can be configured through ApplicationConfigurator
using the following options:
flash.cookie_name
-> Name of the cookie used to store flash messagesflash.default_status
-> Default message status if not specified (ok
by default)flash.template
-> string.Template
instance used as the flash template when
rendered from server side, will receive $container_id
, $message
and $status
variables.flash.allow_html
-> Turns on/off escaping in flash messages, by default HTML is not allowed.flash.js_call
-> javascript code which will be run when displaying the flash
from javascript. Default is webflash.render()
, you can use webflash.payload()
to retrieve the message and show it with your favourite library.flash.js_template
-> string.Template
instance used to replace full
javascript support for flash messages. When rendering flash message for javascript usage
the following code will be used instead of providing the standard webflash
object.
If you replace js_template
you must also ensure cookie parsing and delete it for
already displayed messages. The template will receive: $container_id
,
$cookie_name
, $js_call
variables.message
¶Get only current flash message, getting the flash message will delete the cookie.
pop_payload
()¶Fetch current flash message, status and related information.
Fetching flash message deletes the associated cookie.
render
(container_id, use_js=True)¶Render the flash message inside template or provide Javascript support for them.
container_id
is the DIV where the messages will be displayed, while use_js
switches between rendering the flash as HTML or for Javascript usage.
status
¶Get only current flash status, getting the flash status will delete the cookie.