Introduction and Creating an Application

Following along with the examples

The examples in this guide are based on (A) Pyramid 1.3's default SQLAlchemy application and (B) the Akhet demo. (Akhet is an add-on package containing some Pylons-like support features for Pyramid.) Here are the basic steps to install and run these applications on Linux Ubuntu 11.10, but you should read Creating a Pyramid ProjectPyramid manual before doing so:

 1# Prepare virtual Python environment.
 2
 3$ cd ~/workspace
 4$ virtualenv myvenv
 5$ source myvenv/bin/activate
 6(myvenv)$ pip install 'Pyramid>=1.3'
 7
 8# Create a Pyramid "alchemy" application and run it.
 9
10(myvenv)$ pcreate -s alchemy PyramidApp
11(myvenv)$ cd PyramidApp
12(myvenv)$ pip install -e .
13(myvenv)$ initialize_PyramidApp_db development.ini
14(myvenv)$ pserve development.ini
15Starting server in PID 3871.
16serving on http://0.0.0.0:6543
17
18# Press ctrl-C to quit server
19
20# Check out the Akhet demo and run it.
21
22(myvenv)$ git clone git://github.com/mikeorr/akhet_demo
23(myvenv)$ cd akhet_demo
24(myvenv)$ pip install -e .
25(myvenv)$ pserve development.ini
26Starting server in PID 3871.
27serving on http://0.0.0.0:6543
28
29# Check out the Pyramid source and Akhet source to study.
30
31(myvenv)$ git clone git://github.com/pylons/pyramid
32(myvenv)$ git clone git://github.com/pylons/akhet
33
34(myvenv)$ ls -F
35akhet/
36akhet_demo/
37PyramidApp/
38pyramid/
39myvenv/

*要查找的内容:*页面右上角的“dt”图标是调试工具栏,而 Pylons 没有。“填充 Pyramid ”脚本(第13行)创建数据库。如果你跳过这一步,你会在主页上得到一个例外;你可以“意外地”这样做来查看 Pyramid 的交互式回溯。

The p* Commands

Pylons 使用第三方实用程序 paster 创建和运行应用程序。 Pyramid 将这些子命令替换为一系列以“p”开头的顶级命令:

塔柱

Pyramid

描述

告诫

粘贴创建

帕盖特

创建一个应用程序

Option -s instead of -t

贴纸服务

帕塞尔

Run app based on INI file

-

贴片壳

壳牌

Load app in Python shell

Fewer vars initialized

paster setup-app

populate_App

Initialize database

"App" is application name

贴纸路径

普劳特斯

列出路线

-

-

普韦恩斯

列出吐温

-

-

pVIEW

列表视图

-

在许多情况下,代码是相同的,只是复制到Pyramid中并使python 3兼容。Paste还没有移植到Python3,Pyramid开发人员认为它包含了太多的遗留代码,使移植变得值得。所以他们只是把他们需要的零件装上了门。但是请注意,PasteDeploy is 移植到python 3,Pyramid使用它,我们将在下一章中看到。同样,之前由粘贴产生的其他几个包(如webob)已经移植到了python 3,pyramid仍然使用它们。(Pyramid的开发者们把它们移植了进来。)

脚手架

Pylons 有一个贴纸模板,询问您要创建哪种应用程序。 Pyramid 不会问问题,而是提供多种脚手架供选择。 Pyramid 1.3包括以下脚手架:

Routing mechanism

数据库

Pyramid scaffold

URL调度

SQLAlchemy

炼金术

URL调度

-

起动装置

遍历

ZODB

ZODB

前两个脚手架离 Pylons 最近,因为它们使用URL调度,这类似于路由。它们之间唯一的区别是是否为您配置了一个SQLAlchemy数据库。第三个支架使用 Pyramid 的其他路由机制,遍历。我们在本指南中不介绍遍历,但它在允许用户在任意深度创建URL的应用程序中很有用。URL调度更适合具有固定深度的URL层次结构的应用程序。

要了解其他类型的Pyramid应用程序是可能的,请查看kotti和ptah分布。Kotti是一个内容管理系统,它是使用SQLAlchemy进行遍历的一个例子。

Directory Layout

默认的“炼金术”应用程序在创建和安装后包含以下文件:

 1PyramidApp
 2├── CHANGES.txt
 3├── MANIFEST.in
 4├── README.txt
 5├── development.ini
 6├── production.ini
 7├── setup.cfg
 8├── setup.py
 9├── pyramidapp
10│   ├── __init__.py
11│   ├── models.py
12│   ├── scripts
13│   │   ├── __init__.py
14│   │   └── populate.py
15│   ├── static
16│   │   ├── favicon.ico
17│   │   ├── pylons.css
18│   │   ├── pyramid.png
19│   ├── templates
20│   │   └── mytemplate.pt
21│   ├── tests.py
22│   └── views.py
23└── PyramidApp.egg-info
24    ├── PKG-INFO
25    ├── SOURCES.txt
26    ├── dependency_links.txt
27    ├── entry_points.txt
28    ├── not-zip-safe
29    ├── requires.txt
30    └── top_level.txt

(我们省略了一些静态文件。)如您所见,目录结构类似于 Pylons ,但不完全相同。