与您一起使用扩展的Web框架
TurboGears2建立在几个下一代web框架的基础上,这些框架包括TurboGears1(第一个版本)、Django和Rails。所有这些框架都有令我们沮丧的局限性,而TurboGears2正是为了应对这种挫折而构建的。备注tg是简称
TurboGears2安装
$pip install TurboGears2
1.基于TG简单web
1.1源代码demo.py
代码要实现页面显示“Hello keny大成”
from wsgiref.simple_server import make_server
from tg import MinimalApplicationConfigurator
from tg import expose, TGController
# RootController of our web app, in charge of serving content for /
class RootController(TGController):
@expose(content_type="text/plain")
def index(self):
return 'Hello keny大成'
# Configure a new minimal application with our root controller.
config = MinimalApplicationConfigurator()
config.update_blueprint({
'root_controller': RootController()
})
# Serve the newly configured web application.
print("Serving on port 8080...")
httpd = make_server('', 8080, config.make_wsgi_app())
httpd.serve_forever()
1.2启动效果
$python demo.py
Serving on port 8080...
Default renderer not in renders, automatically switching to json
app_globals not provided and lib.app_globals.Globals is not available.
helpers not provided and lib.helpers is not available
验证的环境是正常.可以开始下面步骤
2.安装tg.devtools:(TurboGears2工具的缩写)
$pip install tg.devtools
......
Successfully installed Beaker-1.11.0 Kajiki-0.8.2 Mako-1.1.2 WebHelpers2-2.0 alembic-1.4.2 formencode-2.0.0a1 nine-1.1.0 python-editor-1.0.4 repoze.who-2.3 speaklater-1.3 sprox-0.11.1 tgext.admin-0.7.4 tgext.crud-0.9.0 transaction-3.0.0 tw2.core-2.2.9 tw2.forms-2.2.6 wiki20 zope.interface-5.1.0 zope.sqlalchemy-1.3
安装工具,等待时间比较长,请确保网络环境。
2.1 20分钟内开始建立wiki应用
我们将通过开发一个简单的wiki应用程序向您展示,该应用程序的完成时间不应超过20分钟
$gearbox quickstart kenywiki
快速建立kenywiki应用 命令格式就是 :gearbox quickstart project_name
2.2 进入到kenywiki目录安装各种包
$cd kenywiki
$pip install -e .
注意:这个-e后面的"."字符
...安装各种包...
Successfully installed Beaker-1.11.0 Kajiki-0.8.2 Mako-1.1.2 WebHelpers2-2.0 alembic-1.4.2 formencode-2.0.0a1 nine-1.1.0 python-editor-1.0.4 repoze.who-2.3 speaklater-1.3 sprox-0.11.1 tgext.admin-0.7.4 tgext.crud-0.9.0 transaction-3.0.0 tw2.core-2.2.9 tw2.forms-2.2.6 wiki20 zope.interface-5.1.0 zope.sqlalchemy-1.3
2.3 运行kenywiki
确保在kenywiki目录下运行
gearbox serve --reload --debug
运行状态提示开启debug模式,日志会比较多,方便查看
12:30:54,771 INFO [tgext.debugbar] Enabling Debug Toolbar
12:30:54,834 INFO [gearbox] Starting server in PID 1061.
12:30:54,839 INFO [gearbox] Starting Standard HTTP server on http://127.0.0.1:8080
2.4 kenywiki运行效果
全部英文界面,建立的时候,还没有到20分钟
2.5 总结 kenywiki,20分钟完成
$ virtualenv --no-site-packages tg2env
$ cd tg2env/
$ source bin/activate
(tg2env)$ pip install tg.devtools
(tg2env)$ gearbox quickstart kenywiki
(tg2env)$ cd example/
(tg2env)$ pip install -e .
(tg2env)$ gearbox serve
3.目录结构
3.1kenyWiki目录结构为
kenyWiki
├── __init__.py <--
├── config <-- 项目设置和配置所依赖的位置
├── controllers <-- web项目的控制层
├── i18n <-- 转换各种的语言包
├── lib <-- python库包
├── model <-- 数据库模板
├── public <-- 静态文件 like CSS, javascript and images
├── templates <-- 控制器公开的模板.
├── tests <-- 测试集
└── websetup <-- 在应用程序设置时执行的函数。像创建表、标准用户等等。
3.2完整目录结构说明
3.3登录
登录账号密码需要在进行生成
Authentication & Authorization in a TG2 site.
gearbox setup-app
16:03:45,865 INFO [tgext.debugbar] Enabling Debug Toolbar
Creating tables
Initializing Migrations
16:03:45,993 INFO [alembic.runtime.migration] Context impl SQLiteImpl.
16:03:45,993 INFO [alembic.runtime.migration] Will assume non-transactional DDL.
没有运行setup-app的提示
3.3.1账号密码
管理账号和密码 :manager/ managepass
编辑账号和密码:editer/editpass
4.汉化
4.1 欢迎界面和导航栏
如果想修改密码和账号信息;可以在websetup.py文件修改
4.2环境
如果还需要增加内容,需要编写数据模型的代码设计数据模型,创建数据库,并添加一些引导数据。
4.3登录界面
4.4 登录账号管理
4.3.1 用户,权限和组,增加修改删除;(注意:managers是核心组,不要删除)
4.3.2 增加用户组AdminGroups
点击Group新加一个组名称为AdminGroups
4.3.3 增加用户Admin
点击User新加一个用户为Admin
4.3.4权限Admin设置
点击Permission,设置权限,选择输入组AdminGroup和mangers组
备注:一个用户允许关联多个组权限
5.参考的文档
https://turbogears.readthedocs.io/en/latest/turbogears/wiki20.html#initializing-the-tables