2010年10月22日 星期五 17:10
3. Getting Started
Now that you have seen to how to set up a Python environment and have used Easy Install to install Pylons and its dependencies it is time to create your first Pylons project. In time-honoured tradition we'll start with an example which outputs the words Hello world! to the web browser.
现在您已经知道如何建立Python环境,并已使用Easy Install安装Pylons及其依赖,现在是时候建立您的第一个Pylons项目。按照历史悠久的传统,我们将开始一个例子,输出Hello world!到web浏览器。
In this chapter you'll also learn the basics of the HyperText Transfer Protocol, get to grips with Pylons' request and response objects and begin learning how Pylons works and about the other objects Pylons' sets up for you.
在这一章中您也可以了解超文本传输协议的基础知识,开始对付Pylons的request 和response对象,并开始学习Pylons如何工作的及Pylons建立的其它对象。
As you learned in Chapter 1 Introducing Pylons, Pylons is more like a collection of very carefully chosen separate components than a tightly integrated framework but it can often be difficult to work out exactly how each of the components fits together. You'll learn this over the course of the book but let's take a quick look at the packages which were installed along with Pylons so that you can get an idea of the big picture.
正如你在第1章Pylons介绍学到的,Pylons更像是精心挑选的组件集合,而不是一个紧密集成的框架,但它往往是很难弄明白到底每一个组件是如何合适地在一起的。您将这本书中了解这些,但先让我们快速看一下随Pylons一起安装的软件包,让您可以获得一个框架的全貌。
If you followed the installation instructions from the previous chapter have a look at the lib/python2.5/site-packages directory inside your virtual Python environment. You will see all of Pylons' dependencies as well as the easy-install.pth and setuptools.pth which EasyInstall uses to keep track of the installed eggs. If you've installed other pacakges too you will also see them.
如果您遵循从上一章的安装指示,看一下虚拟的Python环境中的lib/python2.5/site-packages目录内。你会看到所有的Pylons依赖,以及easy-install.pth和setuptools.pth,EasyInstall使用它们追踪安装的eggs。如果您已安装过其他pacakges,你也会看到它们。
Each of the components relevant to Pylons 0.9.7 is listed below. Of course the version numbers might change slightly over time and future versions of Pylons might have slightly different dependencies but the list below is correct at the time of writing.
有关Pylons 0.9.7的每一个组件在下面列出。当然,随着时间的推移,版本号码可能会略有变化,未来版本的Pylons可能会略有不同的依赖,但下面的列表在写作期间是正确的。
Beaker-0.9.5-py2.5.egg
Beaker是一个软件包,Pylons将之用于内部,实现其高速缓存(caching)和会话(session)功能。Pylons的session功能会在稍后的章节中介绍使用Beaker实现,如同Pylons 的caching功能在食谱(cookbook)上描述的
http://wiki.pylonshq
FormEncode-1.0.1-py2.5.egg
FormEncode是验证来自网站的表单提交的软件库。尽管它不是用于Pylons本身,但是因为它经常被Pylons的用户使用,也被认为是Pylons的一个重要组成部分。FormEncode软件包还包含了formencode.htmlfill模块,可用于生成包含值和错误信息的HTML表单域的字符串。在Pylons应用中,FormEncode和HTMLFill组成了一个理想的工具集来处理表单。整个第6章“表单和校验”是专门用于解释如何在Pylons应用使用FormEncode和HTMLFill。
Mako-0.2.0-py2.5.egg
Mako是Pylons 0.9.7支持的直接可用的三个模板语言之一。其他的两个是Genshi(一个XML模板语言)和Jinja(基于Django的模板系统)。如果你要使用它们的话,你必须单独安装Genshi和Jinja,而Mako包括在Pylons的默认安装,因为它是推荐使用的模板语言。用Mako来生成你的视图(view)在第5章“视图模板”有详细的说明。
nose-0.10.3-py2.5.egg
nose提供工具来帮助你编写和运行自动单元测试。测试在第12章“测试”中描述。
Paste-1.6-py2.5.egg, PasteDeploy-1.3.2-py2.5.egg and PasteScript-1.6.3-py2.5.egg
Paste由三个软件包组成,有利于那些只需要一个部分功能的框架开发者。Pylons使用所有三个包,用于整个框架中各种各样的不同的东西,但再次,作为一个Pylons应用开发者,你通常不会直接与Paste组件互动。
Over time the functionality in the paste modules is being split up into custom packages. For example the paste.wsgiwrappers module which provided the pylons.request and pylons.response objects in Pylons 0.9.6 is now replaced by WebOb which provides the Pylons 0.9.7 versions of those Pylons objects. The paste.eval_exception module which provided the 0.9.6 error handling is replaced by WebError in Pylons 0.9.7 and even the paste.auth functionality has been built upon and improved in AuthKit which you'll learn about in Chapter 18 Authentication and Authorization. Don't be surprised if future versions of Pylons include even more projects spun out from their roots in Paste.
随着时间的推移,Paste模块的功能正在分成小部分定制封装。例如,在Pylons 0.9.6,paste.wsgiwrappers模块提供了pylons.request和pylons.response对象,现在被WebOb在Pylons 0.9.7中取代并负责提供这些对象。paste.eval_exception模块提供了在0.9.6中的错误处理,被WebError在Pylons 0.9.7取代,甚至paste.auth功能已在AuthKit基础上建立和改善,你将会在第18章“验证和授权”了解到。不要惊讶,如果未来版本的Pylons甚至包括更多的项目,改变的根源在于Paste。
Despite the gradual shift to separate packages Pylons still relies on Paste for its configuration files, registry manager, development HTTP server, project template creation, test fixtures, error documents and more. The various parts of Paste are described throughout the book as they are encountered.
尽管逐渐转变为单独的软件包,在配置文件,注册管理, HTTP开发服务器,项目模板创建,测试装置,错误文档等方面,Pylons仍然依靠Paste。Paste的各部分将在整本书中遇到并叙述。
Pylons-0.9.7-py2.5.egg
这就是用来粘合Pylons的其他组件所需的一切。Pylons本身是比较小,所以如果你好奇的话,随时看看它的代码来了解一切是如何具体工作。
Routes-1.9-py2.5.egg
Pylons采用了Routes的系统,允许你将URL映射成一组变量,通常包括控制器(controller)和行为(action)。这些变量用于确定是哪些Pylons控制器类和方法应该用来处理请求。同时,Routes可以让你指定一组变量并由这些变量生成URL,从而使你再也不需要在你的应用中硬编码URL。我们会在这一章介绍路线,但你将在第9章“URL,路由和调度”详细了解Route的所有强大功能。
setuptools-0.6c8-py2.5.egg
Setuptools包含的方法,被easy_install脚本使用,提供所有的功能并允许使用egg文件。
simplejson-1.8.1-py2.5-linux-x86_64.egg
这个软件包用于在JSON和Python格式之间转换数据,前面提到的@jsonify装饰器使用了它。Pylons应用开发者有时还直接在其控制器(controller)中使用simplejson。 JSON支持JavaScript对象符号,它是一个数据结构的格式,很容易解析JavaScript。这使得它在AJAX设置从Pylons应用发送数据到web浏览器很有用。
Tempita-0.2-py2.5.egg
Tempita是一个很小的模板语言,它是Paste的一个依赖包。它只是当你用paster create命令(本章节稍后说明)创建一个新的Pylons项目目录,幕后简单的变量替换。
WebError-0.8-py2.5.egg
WebError为Pylons提供强大的交互式调试和跟踪功能,将在第4章“追踪和处理问题”描述。
WebHelpers-0.6-py2.5.egg
WebHelpers是一个独立的函数(function)和类(class)的集合,提供了很多有用的功能,如生成常见的HTML标签和表单字段,处理多个页面的结果,及其它更多的工作。
WebOb-0.9.2-py2.5.egg
在Pylons 0.9.7中,提供新的pylons.request和pylons.response对象。
You might have noticed that SQLAlchemy a database toolkit you'll learn about in Chapter 7 The Model and SQLAlchemy and AuthKit a toolkit you'll learn about in Chapter 18 Authentication and Authorization, are not included in the list of packages installed automatically with Pylons. This is because Pylons can be used perfectly well without them and although most users will choose to install them some developers will want to choose alternatives instead.
你可能已经注意到,SQLAlchemy(一个数据库工具包,你将在第7章“模型和SQLAlchemy”了解到)和AuthKit(一个工具包,你将在第18章“验证和授权”了解到),没有列入Pylons自动安装的软件包名单中。这是因为在没有它们的情况下,Pylons一样工作地很好,尽管大多数用户会选择安装它们,但是一些开发者也会选择其它的方案代替它们。
Installing Pylons also installs some scripts. If you look in your vitual Python environment's bin directory (or Scripts directory on Windows) you will see the following:
安装Pylons时也会安装一些脚本。如果你看看在你的虚拟的Python环境中的bin目录(或在Windows中的Scripts目录),你将看到以下一些命令:
activate (or activate.bat on Windows)
启用(或在Windows中的activate.bat )
nosetests
该脚本用于运行你的Pylons测试(tests),它是由上述的nose软件包提供,nose软件包将在第12章“测试”描述。
python
Python可执行程序,你应该会在虚拟的Python环境所有的工作范围内使用它。
easy_install
EasyInstall程序用于安装软件到你的虚拟环境中,这在第2章“安装”已描述。
mako-render
随Mako一起安装的一个简单的脚本,它将一个包含Mako模板标记的单独文件作为参数,输出渲染后的模板到控制台的标准输出。这对Pylons的开发不是很有用。
paster
A very useful script which makes use of the PasteScript package and has a number of sub commands including paster create and paster serve which we'll see later in this chapter which are for creating a new Pylons project and serving a Pylons application respectively. We'll also see paster make-config and paster setup-app which are for handling the creation of a config file from a distributed Pylons project and for setting it up. These are advanced features we'll learn about in the SimpleSite Tutorial Chapters.
一个非常有用的脚本,利用PasteScript软件包,有若干子命令,其中包括paster create和paster serve,我们在本章稍后可以看到,它们分别用于建立一个新的Pylons项目和启动Pylons应用服务。我们还可以看到paster make-config和paster setup-app,用于建立一个用于分布式Pylons项目的配置文件和设置该配置文件。这些高级的功能我们会在SimpleSite教程章节中了解到。
Your bin (or Scripts) directory might also see a file such as easy_install-2.5 which is simply a Python 2.5 version of the easy_install script or a python2.5 script which is a Python 2.5 version of the python script if you are using multiple version of Python on your system. You should generally use the easy_install and python versions as these match the version of Python you used when you ran the python virtual_python.py env command in Chapter 2.
如果你在你的系统中使用的是多个版本的Python,你的bin(或Scripts)目录可能会看到这样的文件,如easy_install-2.5,这只是一个Python 2.5版本的easy_install脚本,或者python2.5脚本,这是一个Python2.5版本的python脚本。当你运行python virtual_python.py env在第2章提过的命令时,你一般应该使用有版本的easy_install和python,因为这些匹配Python版本。
Don't worry if you don't understand everything which has been mentioned above, it will all become clear as you become more familiar with Pylons.
如果你不理解如上所述的一切,不要担心,当你更加熟悉Pylons,它将会全部变得明朗。
3. 入门
Exploring Pylons' Dependencies
探索Pylons的依赖包
Beaker is a piece of software used internally by Pylons to implement its caching and session functionality. The Pylons session global described later in the chapter makes use of Beaker as does Pylons' caching functionality described in the cookbook at
http://wiki.pylonshq.com/display/pylonsdocs/Caching+in+Templates+and+Control
-
lers but you would never normally interact with Beaker yourself directly.
.com/display/pylonsdocs/Caching+in+Templates+and+Controllers,但你通常不会直接与Beaker交互。
decorator-2.2.0-py2.5.egg
This is a simple tool which is used by Pylons to create the @validate and @jsonify decorators. You'll learn about @validate in Chapter 6 Forms and Validators and you'll learn about @jsonify in Chapter 15 JavaScript and Ajax. Once again you would not normally use decorator in your own programs because you would usually use the decorators provided by Pylons.
这是一个简单的工具,Pylons将之用于建立@validate和@jsonify装修器(decorator)。你将分别会在第6章“表单和校验”了解@validate,在第15章“JavaScript和Ajax”了解@jsonify。再次,你通常不会在自己的程序中用到decorator,因为你通常会使用Pylons所提供的decorator。
FormEncode is a library for validating form submission from websites. Although it isn't used by Pylons itself it is used so frequently by Pylons users that it is considered an essential part of Pylons. The FormEncode package also includes a module named formencode.htmlfill which can be used to populate a string containing HTML fields with values and error messages. Together FormEncode and HTMLFill make an ideal toolset for handling forms in a Pylons application. The whole of Chapter 6 Forms and Validators is dedicated to explaining how to use FormEncode and HTMLFill in a Pylons application.
Mako is one of the three template languages which Pylons 0.9.7 supports out of the box. The others are Genshi (an XML template language) and Jinja (based on Django's template system). You have to install Genshi and Jinja separately if you want to use them whereas Mako is included in the default Pylons installation because it is the recommended template language to use. Using Mako to generate your views is described in detail in Chapter 5 View Templates.
nose provides tools to help you write and run automated unit tests. Testing is described in Chapter 12 Testing.
Paste comes in three packages for the benefit of framework developers who only require one part of its functionality. Pylons uses all three packages for a wide variety of different things throughout the framework but once again, as a Pylons application developer you wouldn't normally directly interact with the Paste components yourself.
This is where everything needed to glue together the other components of Pylons is found. Pylons itself is relatively small so if you are the curious type feel free to have a look at its code to get a feel for how everything works.
Pylons uses a system called Routes which allows you to map a URL to a set of variables which usually include controller and action. These variables are then used to determine which Pylons controller class and method should be used to handle the request. At the same time Routes allows you to specify a set of variables and have a URL generated from them so that you never need to hard code URLs into your application. We introduce Routes in this chapter but you will learn the details of all of Route's powerful features in Chapter 9 URLs, Routing and Dispatch.
Setuptools contains the methods used by the easy_install script to provide all of its features and allow the use of egg files.
This package converts data back and forth between JSON and Python formats and is used by the @jsonify decorator mentioned earlier. Pylons application developers also occasionally use simplejson directly in their controllers. JSON stands for JavaScript Object Notation and is a data structure format which is very easy to parse in JavaScript. This makes it useful in AJAX setups for sending data from a Pylons application to a web browser.
Tempita is a small template language which is dependency of Paste. It is only used behind the scenes for simple variable substitutions when you create a new Pylons project directory with the paster create command described later in this chapter.
WebError provides Pylons powerful interactive debugging and traceback functionality described in Chapter 4 Tracking Down and Handling Problems.
WebHelpers are a collection of stand-alone functions and classes which provide useful functionality such as generating common HTML tags and form fields, handling multiple pages of results and much more.
Provides the new pylons.request and pylons.response objects in Pylons 0.9.7.
An optional script described in Chapter 2 Installation for activating a virtual Python environment to make the other scripts available automatically on the current shell or command prompt without having to type the full path to the virtual Python environment.
一个可选的脚本,在第2章“安装”中讲述过,启动一个虚拟的Python环境,使其它脚本可在当前shell或命令提示符直接输入,而不必输入到虚拟的Python环境的完整路径。
A script used for running your Pylons tests which is provided by the nose package mentioned above and described in Chapter 12 Testing.
The Python executable you should use for all work within the virtual Python environment.
The EasyInstall program for installing software into your virtual environment described in Chapter 2 Installation
A simple script installed with Mako which takes a single file containing Mako template markup as an argument and outputs the rendered template to the standard output on your console. This isn't very useful for Pylons development.
原文: http://hi.baidu.com/zhuifengdenanhai/blog/item/7eb31fdf3948e6146227982f.html
Zeuux © 2024
京ICP备05028076号