- TurboGears 教程
- TurboGears - 主页
- TurboGears - 概述
- TurboGears - 环境
- TurboGears - 第一个程序
- TurboGears - 依赖关系
- TurboGears - 服务模板
- TurboGears - HTTP 方法
- Genshi模板语言
- TurboGears - 包括
- TurboGears - JSON 渲染
- TurboGears - URL 层次结构
- TurboGears - Toscawidgets 表格
- TurboGears - 验证
- TurboGears - 闪讯
- TurboGears - Cookie 和会话
- TurboGears - 缓存
- TurboGears - Sqlalchemy
- TurboGears - 创建模型
- TurboGears - 原油操作
- TurboGears - 数据网格
- TurboGears - 分页
- TurboGears - 管理员访问
- 授权与认证
- TurboGears - 使用 MongoDB
- TurboGears - 脚手架
- TurboGears - 挂钩
- TurboGears - 编写扩展
- TurboGears - 可插拔应用
- TurboGears - 安静的应用程序
- TurboGears - 部署
- TurboGears 有用资源
- TurboGears - 快速指南
- TurboGears - 有用的资源
- TurboGears - 讨论
TurboGears – 闪讯
TurboGears 提供了一个非常方便的消息系统,以非侵入性的方式向用户通知信息。tg 模块中的 TGFlash 类提供对存储在普通 cookie 中的闪烁消息的支持。该类支持通过 JavaScript 在服务器端和客户端获取 Flash 消息。
当从 Python 本身使用时,可以从模板调用 TGFlash 类的 render() 方法来呈现 Flash消息。如果在 JavaScript 上使用,它会提供一个 WebFlash 对象。它公开了payload()和render()方法来获取当前的flash消息并从JavaScript中渲染它。
使用“快速启动”创建 TurboGears 项目时,它有一个 Master.html 模板。它包含该 flash 对象的变量的定义。从控制器接收到的此闪存消息的内容替换此模板中标记的占位符。
<py:with vars = "flash = tg.flash_obj.render('flash', use_js = False)">
<div py:if = "flash" py:replace = "Markup(flash)" />
</py:with>
tg.flash_obj是 WebFlash 对象,通过包含master.html模板,它可以在任何呈现的模板中使用。该对象允许检索当前的闪存消息并显示它。
Flash 消息通过使用tg.flash()方法存储在 cookie 中(默认名称为 webflash)。然后将消息和状态参数传递给它。
tg.flash('Message', 'status')
如果调用 flash 的方法执行重定向,则 flash 将在重定向页面内可见。如果该方法直接公开模板,则 Flash 在模板本身内部将可见。
可以通过将 CSS 样式应用于状态代码来自定义 Flash 消息的外观。“快速启动”项目包含由样式表 public/css/style.css 定制的错误、警告、信息和正常状态代码。还可以添加更多带有样式的状态代码。
#flash > .warning {
color: #c09853;
background-color: #fcf8e3;
border-color: #fbeed5;
}
#flash > .ok {
color: #468847;
background-color: #dff0d8;
border-color: #d6e9c6;
}
#flash > .error {
color: #b94a48;
background-color: #f2dede;
border-color: #eed3d7;
}
#flash > .info {
color: #3a87ad;
background-color: #d9edf7;
border-color: #bce8f1;
}
该外部样式表需要包含在模板中 -
<link rel = "stylesheet" type = "text/css" media = "screen"
href = "${tg.url('/css/style.css')}" />
任何 Flash 消息支持的配置都可以通过设置 TGFlash 对象的 configure() 方法或 app_cfg.py(在 config 文件夹中)中的参数来实现。可配置的参数是 -
| 先生。 | 参数及说明 |
|---|---|
| 1 | flash.cookie_name 用于存储 flash 消息的 cookie 的名称。默认是webflash。 |
| 2 | flash.default_status 如果未指定则默认消息状态(默认情况下可以) |
| 3 | flash.模板 渲染时用作Flash 模板。 |
| 4 | flash.allow_html 打开/关闭 Flash 消息中的转义,默认情况下不允许 HTML。 |
| 5 | flash.js_call 从 JavaScript 显示 Flash 时将运行的 JavaScript 代码。默认是webflash.render() |
| 6 | flash.js_模板 string.Template实例用于替换对 Flash 消息的完整 JavaScript 支持。 |
pop_payload() - 函数获取当前闪存消息、状态和相关信息。获取闪现消息将删除 cookie。
render(container_id, use_js=True) - 在模板内渲染 Flash 消息或为其提供 Javascript 支持。
container_id是显示消息的 DIV,而 use_js 在将 Flash 渲染为 HTML 或供 JavaScript 使用之间切换。
status - 仅获取当前闪存状态,获取闪存状态将删除 cookie。
message - 仅获取当前的flash 消息,获取flash 消息将删除cookie。
如何制作简单的Flash消息?
在以下示例中,根控制器类中提供了 flash() 方法。它调用 flash() 消息,该消息被渲染到公开的模板 flash.html
from hello.lib.base import BaseController
from tg import expose, flash, redirect, request
class RootController(BaseController):
@expose('hello.templates.flash')
def flash(self, user = None):
if user:
flash(message = "Welcome "+user,status = "ok")
else:
flash(message = "Welcome Guest",status = "info")
return {}
templates文件夹下制作flash.html的代码如下
<html xmlns = "http://www.w3.org/1999/xhtml"
xmlns:py = "http://genshi.edgewall.org/"
xmlns:xi = "http://www.w3.org/2001/XInclude">
<head>
<title>TurboGears 2.3: Flash messages>/title>
<link rel = "stylesheet" type = "text/css" media = "screen"
href = "${tg.url('/css/style.css')}" />
<py:with vars = "flash = tg.flash_obj.render('flash', use_js = False)">
<div py:if = "flash" py:replace = "Markup(flash)" />
</py:with>
</head>
<body>
<h2>Hello TurboGears</h2>
</body>
</html>
启动服务器,在浏览器中输入http://localhost:8080/flash?user=MVL
将 URL 更改为http://localhost:8080/flash并根据 style.css 中的定义查看不同格式的 Flash 消息
