本文是Python TurboGears 2框架的入门文档《Whetting the Appetite: Make a Wiki in 20 Minutes》的简单翻译,并对一些文档中笔误或出错的地方做简单的校正。
原文地址:Whetting the Appetite: Make a Wiki in 20 Minutes
※注意:因TurboGears 项目是一个 “大框架(megaframework)”,也就是说它是由现有的子项目构成的。所以,不同版本的TurboGears 会采用不同的组件集成(你也可以自定义),由此也产生一些因组件升级,导致用法不同的问题。上文的链接导向最新的文档说明,可能在您看到该文档时已经有新的改变,请留意。或者,采用下面文档备份:
下载文件 一、安装
TurboGears 2 基于Python 2.6 或 2.7版本,可通过pip 安装。这里以红旗Asianux 4.0 SP2 为操作平台说明。
1. 安装pip
2. 安装独立运行环境
为了避免污染现有运行环境,这里安装一个虚拟运行环境,然后再安装TurboGears 2框架。
# virtualenv tg22env
# . tg22env/bin/activate
由此,将进入虚拟运行系统中:
3. 安装TurboGears2
这将从官网下载最新的TurboGears2 ,我当前使用的是TurboGears2-2.2.2-py2.6.egg-info 版本。
二、快速开始
1. 创建基础架构
TurboGears2 提供一整套的工具paster 协助开发,可用于快速搭建基础平台:
Enter project name: Wiki 20
Enter package name [wiki20]:
Would you prefer to use an alternative template system? (m=mako, j=jinja, k=kajiki, n=no [default]):
Do you need authentication and authorization in this project? ([yes]/no):
构建的目录如下:
其中,Wiki-20 是项目根路径位置,其内容如下:
├── development.ini <- 开发环境相关配置文件
├── ez_setup
├── MANIFEST.in
├── migration
├── README.txt
├── setup.cfg
├── setup.py <- 配置脚本
├── setup.pyc
├── test.ini
├── wiki20 <- 项目位置
└── Wiki_20.egg-info
wiki20 是项目名称,其内容如下:
├── __init__.py
├── config <-- 项目配置文件
├── controllers <-- 存放控制器文件目录
├── i18n <-- 本土化多语言支持
├── lib <-- Python 库函数和类
├── model <-- 数据模型文件目录
├── public <-- 公共目录,可存放CSS、JavaScript、图片等
├── templates <-- 模板文件目录
├── tests <-- 测试目录
└── websetup <-- 用于安装配置项目的函数,如创建表、用户等
2. 下载依赖库软件
TurboGears 直接采用第三方的库框架,所以需要下载这些依赖的库软件包。它们是由Wiki-20/setup.py 配置文件决定的,本项目wiki 需使用docutils 类,所以要编辑该文件,内容如下:
“TurboGears2 >= 2.2.2”,
“Genshi”,
“zope.sqlalchemy >= 0.4”,
“repoze.tm2 >= 1.0a5”,
“sqlalchemy<0.8b1",
“sqlalchemy-migrate”,
“repoze.who”,
“repoze.who-friendlyform >= 1.0.4”,
“tgext.admin >= 0.5.1”,
“repoze.who.plugins.sa”,
“tw2.forms”,
“docutils”,
]
蓝色部分是新加的内容,然后用pip 下载安装:
※ 注意:当前路径是Wiki-20 目录。
3. 启动Web 服务
TurboGears2 提供一个建议的Web 服务,可用于快速测试,默然监听在127.0.0.1。可修改development.ini 配置文件:
use = egg:Paste#http
host = 192.168.228.180 # IP 地址
port = 8080
启动服务:
Starting subprocess with file monitor
Starting server in PID 6185.
serving on http://192.168.228.180:8080
可能会有些DeprecationWarning 的报错,这是因为某些库更新后,用法改变导致的,通常影响不大。
※ 注意:运行路径在Wiki-20 根目录下。
打开浏览器,访问http://ip:8080/
其中,@expose 表示使用哪个视图模板:
这里使用的是index.html模板(在templates 目录下),不需指定扩展名。每个控制器都可以使用dict() 方法返回一个字典内容。例如下面对应http://ip:8080/index 动作:
class RootController(BaseController):
secc = SecureController()
admin = AdminController(model, DBSession, config_type=TGAdminConfig)
error = ErrorController()
def _before(self, *args, **kw):
tmpl_context.project_name = “wiki20”
@expose(‘wiki20.templates.index’)
def index(self):
“””Handle the front-page.”””
return dict(page=’index’)
2. 视图模板
视图模板在Wiki-20/wiki20/templates/目录下,它采用XHTML 格式,例如index.html:
new
Read the Getting Started section
${h.icon(‘book’)} TG2 Book
Work in progress TurboGears2 book
${h.icon(‘comment’)} Join the Mail List
for general TG use/topics
Code your data model
Design your data model, Create the database, and Add some bootstrap data.
Design your URL architecture
Decide your URLs, Program your controller methods, Design your
templates, and place some static files (CSS and/or Javascript).
Distribute your app
Test your source, Generate project documents, Build a distribution.
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/98414.html