`
haohappy2
  • 浏览: 326020 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Study Pylons One

阅读更多

安装:
1. 下载安装工具ez_setup.py
2. 命令行运行:python ez_setup.py Pylons
    耐心等待,安装结束。
3. 设置环境变量
    系统变量->path->;C:\Python24\Scripts
4. 命令行运行:paster
    产生下面类似结果,则说明安装成功。
运行结果
Usage: C:\Python24\Scripts\paster-script.py COMMAND
usage: paster-script.py [paster_options] COMMAND [command_options]

options:
  --version         show program's version number and exit
  --plugin=PLUGINS  Add a plugin to the list of commands (plugins are Egg
                    specs; will also require() the Egg)
  -h, --help        Show this help message

Commands:
  create       Create the file layout for a Python distribution
  grep         Search project for symbol
  help         Display help
  make-config  Install a package and create a fresh config file/directory
  points       Show information about entry points
  serve        Serve the described application
  setup-app    Setup an application, given a config file

pylons:
  controller   Create a Controller and functional test for it

    The Controller command will create the standard controller template
    file and associated functional test to speed creation of controllers.

    Example usage::

        yourproj% paster controller comments
        Creating yourproj/yourproj/controllers/comments.py
        Creating yourproj/yourproj/tests/functional/test_comments.py

    If you'd like to have controllers underneath a directory, just include
    the path as the controller name and the necessary directories will be
    created for you::

        yourproj% paster controller admin/trackback
        Creating yourproj/controllers/admin
        Creating yourproj/yourproj/controllers/admin/trackback.py
        Creating yourproj/yourproj/tests/functional/test_admin_trackback.py

  shell        Open an interactive shell with the Pylons app loaded

    The optional CONFIG_FILE argument specifies the config file to use for
    the interactive shell. CONFIG_FILE defaults to 'development.ini'.

    This allows you to test your mapper, models, and simulate web requests
    using ``paste.fixture``.

    Example::

        $ paster shell my-development.ini
 开始动手:
1. 建立一个Pylons工程
    命令行运行:
    F:\python\lab\Pylons>paster create --template=pylons helloworld
    产生下面类似结果:
运行结果
Selected and implied templates:
  pylons#pylons  Pylons application template

Variables:
  egg:      helloworld
  package:  helloworld
  project:  helloworld
Creating template pylons
Creating directory .\helloworld
  Recursing into +egg+.egg-info
    Creating .\helloworld\helloworld.egg-info/
    Copying paste_deploy_config.ini_tmpl_tmpl to .\helloworld\helloworld.egg-inf
o\paste_deploy_config.ini_tmpl
  Recursing into +package+
    Creating .\helloworld\helloworld/
    Copying __init__.py_tmpl to .\helloworld\helloworld\__init__.py
    Recursing into config
      Creating .\helloworld\helloworld\config/
      Copying __init__.py_tmpl to .\helloworld\helloworld\config\__init__.py
      Copying environment.py_tmpl to .\helloworld\helloworld\config\environment.
py
      Copying middleware.py_tmpl to .\helloworld\helloworld\config\middleware.py

      Copying routing.py_tmpl to .\helloworld\helloworld\config\routing.py
    Recursing into controllers
      Creating .\helloworld\helloworld\controllers/
      Copying __init__.py_tmpl to .\helloworld\helloworld\controllers\__init__.p
y
      Copying error.py_tmpl to .\helloworld\helloworld\controllers\error.py
      Copying template.py_tmpl to .\helloworld\helloworld\controllers\template.p
y
    Recursing into docs
      Creating .\helloworld\helloworld\docs/
      Copying index.txt_tmpl to .\helloworld\helloworld\docs\index.txt
    Recursing into i18n
      Creating .\helloworld\helloworld\i18n/
      Copying __init__.py_tmpl to .\helloworld\helloworld\i18n\__init__.py
    Recursing into lib
      Creating .\helloworld\helloworld\lib/
      Copying __init__.py_tmpl to .\helloworld\helloworld\lib\__init__.py
      Copying app_globals.py_tmpl to .\helloworld\helloworld\lib\app_globals.py
      Copying base.py_tmpl to .\helloworld\helloworld\lib\base.py
      Copying helpers.py_tmpl to .\helloworld\helloworld\lib\helpers.py
    Recursing into models
      Creating .\helloworld\helloworld\models/
      Copying __init__.py_tmpl to .\helloworld\helloworld\models\__init__.py
    Recursing into public
      Creating .\helloworld\helloworld\public/
      Copying index.html_tmpl to .\helloworld\helloworld\public\index.html
    Recursing into templates
      Creating .\helloworld\helloworld\templates/
      Copying autohandler to .\helloworld\helloworld\templates\autohandler
    Recursing into tests
      Creating .\helloworld\helloworld\tests/
      Copying __init__.py_tmpl to .\helloworld\helloworld\tests\__init__.py
      Recursing into functional
        Creating .\helloworld\helloworld\tests\functional/
        Copying __init__.py_tmpl to .\helloworld\helloworld\tests\functional\__i
nit__.py
      Copying test_models.py_tmpl to .\helloworld\helloworld\tests\test_models.p
y
    Copying websetup.py_tmpl to .\helloworld\helloworld\websetup.py
  Copying README.txt_tmpl to .\helloworld\README.txt
  Copying development.ini_tmpl to .\helloworld\development.ini
  Copying setup.cfg_tmpl to .\helloworld\setup.cfg
  Copying setup.py_tmpl to .\helloworld\setup.py
Running C:\Python24\python.exe setup.py egg_info
Adding Pylons to paster_plugins.txt
Adding WebHelpers to paster_plugins.txt
2. 运行这个新建的工程
    1. 命令行运行:
        cd helloworld
        paster serve --reload development.ini
    2. 访问http://127.0.0.1:5000/ ,你将看到欢迎页面。
    3. 在helloworld/public目录下创建一个test.html的文件,内容如下:
<html>
    <body>
        Hello World!
    </body>
</html>
     4. 访问http://127.0.0.1:5000/test.html ,你将看到“HelloWorld!”。

3. 禁用调试功能
    将development.ini文件中的:
    #set debug = false
    改成:
    set debug = false

4. 创建一个控制器、修改Routes
    1. 命令行运行:F:\python\lab\Pylons\helloworld>paster controller hello
    2. 修改helloworld/controllers/hello.py,代码如下:

1from helloworld.lib.base import *
2
3class HelloController(BaseController):
4    def index(self):
5        return Response('hello world')

    3. 修改helloworld/config/routing.py,代码如下:

 """
 Setup your Routes options here
 """
 import sys, os
 from routes import Mapper
 
 def make_map(global_conf={}, app_conf={}):
     root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
    map = Mapper(directory=os.path.join(root_path, 'controllers'))
       # This route handles displaying the error page and graphics used in the 404/500
    # error pages. It should likely stay at the top to ensure that the error page is
    # displayed properly.
    map.connect('error/:action/:id', controller='error')
   
    # Define your routes. The more specific and detailed routes should be defined first,
    # so they may take precedent over the more generic routes. For more information, refer
    # to the routes manual @ http://routes.groovie.org/docs/
    map.connect('', controller='hello', action='index')
    map.connect(':controller/:action/:id')
   map.connect('*url', controller='template', action='view')

    return map
    4. 删除public/index.html。访问http://127.0.0.1:5000/hellohttp://127.0.0.1:5000/ ,你将看到“hello world”。

5.模版和请求周期
  1.创建模版文件:helloworld/templates/serverinfo.myt,代码如下:
<p>Hi, here's the server environment: <br />
<% str(request.environ) %></p>

<p>
and here's the URL you called: <% h.url_for() %>
</p>    2. 修改helloworld/controllers/hello.py,代码如下:
from helloworld.lib.base import *

class HelloController(BaseController):
    def index(self):
        return Response('hello world')
    def serverinfo(self):
        return render_response('/serverinfo.myt')
  3.访问http://127.0.0.1:5000/hello/serverinfo ,你将看到下面类似结果:
运行结果
Hi, here's the server environment:
{'HTTP_REFERER': 'http://pylonshq.com/docs/0.9.3/getting_started.html', 'paste.recursive.forward': , 'pylons.routes_dict': {'action': 'serverinfo', 'controller': 'hello', 'id': None}, 'paste.recursive.include': , 'SCRIPT_NAME': '', 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/hello/serverinfo', 'SERVER_PROTOCOL': 'HTTP/1.1', 'QUERY_STRING': '', 'paste.throw_errors': True, 'CONTENT_LENGTH': '', 'HTTP_ACCEPT_CHARSET': 'gb2312,utf-8;q=0.7,*;q=0.7', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1) Gecko/20061010 Firefox/2.0', 'HTTP_CONNECTION': 'keep-alive', 'HTTP_COOKIE': 'helloworld=ef423552a79452b4061d63335840e6690237c4375498846bc40a5e9218518af5', 'SERVER_NAME': '0.0.0.0', 'REMOTE_ADDR': '127.0.0.1', 'pylons.environ_config': {'cache': 'beaker.cache', 'session': 'beaker.session'}, 'paste.expected_exceptions': [], 'wsgi.url_scheme': 'http', 'beaker.cache': , 'paste.config': {'global_conf': {'error_email_from': 'paste@localhost' , 'email_to': 'you@yourdomain.com' , 'debug': 'false', '__file__': 'F:\\python\\lab\\Pylons\\helloworld\\development.ini', 'smtp_server': 'localhost', 'here': 'F:\\python\\lab\\Pylons\\helloworld'}, 'app_conf': {'session_key': 'helloworld', 'package': 'helloworld', 'session_secret': 'somesecret', 'cache_dir': 'F:\\python\\lab\\Pylons\\helloworld/data', 'session_data_dir': 'F:\\python\\lab\\Pylons\\helloworld/data\\sessions', 'cache_data_dir': 'F:\\python\\lab\\Pylons\\helloworld/data\\cache'}}, 'SERVER_PORT': '5000', 'paste.recursive.script_name': '', 'wsgi.input': , 'HTTP_HOST': '127.0.0.1:5000', 'beaker.session': {'_accessed_time': 1164775636.45, '_creation_time': 1164775537.0710001}, 'paste.recursive.include_app_iter': , 'wsgi.multithread': True, 'paste.httpexceptions': , 'HTTP_ACCEPT': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5', 'wsgi.version': (1, 0), 'paste.registry': , 'wsgi.run_once': False, 'wsgi.errors': ', mode 'w' at 0x00ACE0B0>, 'wsgi.multiprocess': False, 'HTTP_ACCEPT_LANGUAGE': 'zh-cn,zh;q=0.5', 'CONTENT_TYPE': '', 'REMOTE_HOST': 'localhost', 'HTTP_ACCEPT_ENCODING': 'gzip,deflate', 'HTTP_KEEP_ALIVE': '300'}

and here's the URL you called: /hello/serverinfo
6. 使用Sessions
    代码片段:
def serverinfo(self):
    session['name'] = 'George'
    session.save()
   return render_response('/serverinfo.myt')
. 控制器变量和模版全局变量
    控制器变量
    1. 修改helloworld/controllers/hello.py,代码如下:
from helloworld.lib.base import *

class HelloController(BaseController):
    def index(self):
        return Response('hello world')
    def serverinfo(self):
        c.value = 2
        return render_response('/serverinfo.myt')        2. 修改helloworld/templates/serverinfo.myt,代码如下:
<p>The value of <tt>c.value</tt> is:
<% c.value %>

    3. 访问http://127.0.0.1:5000/hello/serverinfo ,你将看到“The value of c.value is: 2”。
 
 模版全局变量
 .修改lib/app_globals.py,代码如下: 1class Globals(object):
 
     def __init__(self, defaults, app, **extra):
        self.message = 'Hello'   
        
    def __del__(self):
        """
         Put any cleanup code to be run when the application finally exits
        here.
       """
        pass
 2.修改helloworld/controllers/hello.py,代码如下:

from helloworld.lib.base import *

class HelloController(BaseController):
    def index(self):
        return Response('hello world')
    def serverinfo(self):
        c.value = 2
        return render_response('/serverinfo.myt')   
    def app_globals_test(self):
        resp = Response()
        if g.message == 'Hello':
            resp.write(g.message)
            g.message = 'Hello World!'
        else:
            resp.write(g.message)
        return resp
 3.访问http://127.0.0.1:5000/hello/app_globals_test/ ,你将看到“Hello World!”
参考资料:http://pylonshq.com/docs/0.9.3/getting_started.html

分享到:
评论

相关推荐

    Python库 | Pylons-0.8.2-py2.3.egg

    **Python库 Pylons-0.8.2-py2.3.egg** Pylons是一个基于Python的开源Web框架,旨在提供一个高效、灵活且可扩展的平台,用于开发高性能的Web应用。这个名为"Pylons-0.8.2-py2.3.egg"的文件是一个特定版本(0.8.2)的...

    [James_Gardner]_The_Definitive_Guide_to_Pylons(z-lib.org).rar

    In this book, cofounder and lead developer James Gardner brings you a comprehensive introduction to Pylons, the web framework that uses the best of Ruby, Python, and Perl and the emerging WSGI ...

    借着今天的大好日子,挖一个Pylons教程的坑

    标题中的“Pylons教程”指的是一个关于Python Web框架Pylons的学习资源,可能是系列文章或者教程。Pylons是一个轻量级、高性能的Web框架,它基于Python语言,设计目标是提供一种灵活且可扩展的环境来构建复杂的Web...

    Apress.the.Definitive.Guide.to.Pylons.Dec.2008

    ### Pylons框架详解:《Pylons权威指南》概览与核心知识点 #### 引言 《Pylons权威指南》(Apress.the.Definitive.Guide.to.Pylons.Dec.2008)是一本针对Python Web开发框架Pylons的专业指导书籍。本书由James ...

    Python Web框架Pylons中使用MongoDB的例子

    在Python Web开发中,Pylons是一个轻量级、高性能的框架,它以其高度可定制性而受到开发者喜爱。Pylons采用MVC(Model-View-Controller)设计模式,允许开发者灵活选择不同的库来实现各个层的功能。在本例子中,我们...

    Gardner -- The Definitive Guide to Pylons -- 2008.pdf

    根据提供的文件信息,我们可以推断出这是一本关于Pylons框架的技术书籍,作者是James Gardner,出版于2008年。以下是对该书的关键知识点进行的详细解读。 ### 关键知识点概述 #### 1. **Pylons 框架简介** - **...

    PyPI 官网下载 | Pylons-0.8.2-py2.3.egg

    资源来自pypi官网。 资源全名:Pylons-0.8.2-py2.3.egg

    Gardner -- The Definitive Guide to Pylons -- 2008 -- code.7z

    Gardner -- The Definitive Guide to Pylons -- 2008 -- code.7z

    pylons:Pylons框架,社区在Pylons项目的指导下得到了维护。 与repoze.bfg合并用于金字塔框架

    塔架 Pylons是一个快速的Web应用程序开发框架。 笔记定向塔已与repoze.bfg合并,并且现在处于仅维护模式。 强烈建议新项目从新的合并的Web框架。安装。 如果要从源代码安装,可以运行以下命令: $ python setup.py ...

    Pyramid英文文档.pdf

    Pyramid 是一款现代 Python Web 开发框架,作为 Pylons 框架的升级版本,它不仅继承了原有框架的优点,还进行了诸多改进与增强。根据提供的文件描述,我们可以提炼出以下几个关键知识点: 1. **Pyramid 的独特之处*...

    塔架pylons_VR游戏开发_天空盒子_Skybox_高清_16K_EXR

    可用于UnityVR开发,3D游戏开发,高清天空盒子Skybox素材,游戏环境背景素材,无水印。 让你身临其境的天空盒子,各类题材丰富,都是辛苦搜罗所得的高清exr格式,可以直接用于Unity开发,特别是VR游戏的开发。...

    Cable Supported Bridges: Concept and Design

    (3)the pylons (or towers) supporting the cable system; (4)the anchor blocks (or anchor piers) supporting the cable system vertically and horizontally, or only vertically, at the extreme ends.

    Mako-0.4.1.tar.gz

    pylons默认的模板就是用的它;相比而言,Django 内建的模板引擎,为了维持所谓模板语法的纯粹性和简单性,更纯粹的满足 MVC 模式的规定,牺牲了很多灵活性,一些高级的功能不得不利用 tag 和 filter 来实现,其写法...

    PHP日志系统plog(PHP).zip

    博主介绍说:“日志是一个应用程序的重要组成部分,今天在看pylons对日志的处理时,受到启发,于是plog就诞生了。很多php框架都忽略了日志的重要性(如kohana),往往只是能用,自定义和可扩展性不够,等到程序出了...

    sdidev:Substance D SDI 开发的扩建

    从 GitHub 中检出这个包(必须是 Substance D 团队 Pylons 组织的成员): $ git clone git@github.com:Pylons/sdidev.git cd 进入sdidev目录。 从下载 virtualenv 并将其安装到您的系统 Python (2.7+) 中。 安装...

Global site tag (gtag.js) - Google Analytics