Pylons架构网站开发从0到1

首先说明下这里的从0到1指的是从没有听说过pylons到开发出一个看上去还不错的网站。一个月前,我没有听说过也不知道什么是pylons,HTML只知道一些标签,JavaScript也不怎么懂,由于只倾向于做后端,对前端没有什么兴趣,一个偶然的机会做了一个系统,感觉前端也挺好玩的。我仅以我的经历介绍下经验。

 ------------------------------

HTML + javaScript

Pylons架构

MySQL

 ------------------------------

那就开始吧^_^

1、 首先介绍下pylons的安装:

这里不包括错误的处理过程,错误的处理过程详见这里的几篇文章:

http://blog.csdn.net/doc_sgl/article/category/1544747

参考pylons官方文档,更详细的介绍也请看这里:

http://docs.pylonsproject.org/projects/pylons-webframework/en/latest/gettingstarted.html

1>  下载go-pylons.py

$ curl http://pylonshq.com/download/1.0/go-pylons.py

2>  运行脚本,并指定虚拟化环境要被安装的目录:(这里是安装在mydevenv目录下)

$ python go-pylons.py mydevenv

或者为了进一步隔离系统的python库,添加--no-site-packages选项:

$ python go-pylons.py--no-site-packages mydevenv

这一步就会生成虚拟化环境并安装pylons。

3>  激活虚拟化环境:这一步很重要!

$ sourcemydevenv/bin/activate

4>  下载pylons源码:

$ hg clone http://bitbucket.org/bbangert/pylons/

官方文档上的这个链接失效了,建议在下面的链接手动下载,(可以使用wget)

pylons官网http://www.pylonsproject.org/projects/pylons-framework/download

或者github: https://github.com/Pylons/pylons

5>  告诉setuptools使用pylons目录的新版本:

$ cd pylons

$ python setup.py develop

2、 创建一个pylons工程helloworld:

$ paster create -t pylons helloworld

在运行这个命令的时候会让你选择:

1>  使用哪种模板类型?

我选择的默认类型mako(默认mako)

2>  是否包括SQLAlchemy支持?

因为我所要做的网站用到数据库,所以True.(默认False).

填完True.如果没有错误就恭喜你已经创建了一个helloworld工程。

但是你很有可能会遇到错误,因为我就遇到了。错误的原因是系统的SQLAlchemy的问题,需要系统已经安装了SQLAlchemy,安装方法:

We’ll assume you’ve already installed Pylons and have the easy_install command. At the command line, run:

easy_install SQLAlchemy

然后安装数据库引擎:

easy_install pysqlite # If you use SQLite and Python 2.4 (notneeded for Python 2.5)

easy_install MySQL-python # If you use MySQL

easy_install psycopg2 # If you use PostgreSQL

See the Python Package Index(formerly the Cheeseshop)for other database drivers.

Tip: Checking Your Version

To see which version of SQLAlchemy you have, go to aPython shell and look at

sqlalchemy.__version__ :

>>>import sqlalchemy

>>>sqlalchemy.__version__

0.5.8

说明你的SQLAlchemy已经安装成功,版本是0.5.8

3>运行helloworld

如果没有选择SQLAlchemy的话,就不用在development.ini里配置db的信息了,但是如果你选择了,请一定要配置DB信息。

完成了上面的配置,然后启动服务:

$ cd helloworld
$ paster serve --reload development.ini

在浏览器打开http://127.0.0.1:5000/就可以看到welcome信息了。

下面创建一个基本的helloworld应用:

$ paster controller hello

打开helloworld/controllers/hello.py,就能看到默认的文件内容:

 

import logging
from pylons import request, response, session, tmpl_context as c, url
from pylons.controllers.util import abort, redirect
from helloworld.lib.base import BaseController, render
log = logging.getLogger(__name__)
class HelloController(BaseController):
    def index(self):
        # Return a rendered template
        #return render(’/hello.mako’)
        # or, Return a response
        return ’Hello World’

用浏览器打开http://127.0.0.1:5000/hello/index

就能看到一个显示helloworld的网页,至此我们的helloworld已经完成。

3、 Helloworld的结构:

[root@host /data/helloworld]$tree

|-- MANIFEST.in

|-- README.txt

|-- development.ini

|-- docs

|-- ez_setup.py

|-- helloworld

|-- helloworld.egg-info

|-- setup.cfg

|-- setup.py

`-- test.ini

这里比较重要的就是development.ini文件和helloworld目录。setup.py我用的系统生成的,如果真要修改这个文件等到进阶篇吧。

关于development.ini文件:

这里面比较常用的设置:

网站的地址和端口:

[server:main]
use = egg:Paste#http
 host =127.0.0.1
port = 5000

数据库的信息:

# SQLAlchemy database URL
sqlalchemy.url =mysql://username:password@host:port/database
sqlalchemy.pool_recycle = 3600

还有log信息

关于helloworld子目录下面会详细讲。

4、 helloworld子目录的结构:

[root@host /data/helloworld/helloworld]$tree

|-- __init__.py

|-- __init__.pyc

|-- config

|   |--__init__.py

|   |--__init__.pyc

|   |--deployment.ini_tmpl

|   |--environment.py

|   |--environment.pyc

|   |--middleware.py

|   |--middleware.pyc

|   |--routing.py

|   `--routing.pyc

|-- controllers

|   |--__init__.py

|   `--error.py

|-- lib

|   |--__init__.py

|   |--__init__.pyc

|   |--app_globals.py

|   |--app_globals.pyc

|   |--base.py

|   |--helpers.py

|   `--helpers.pyc

|-- model

|   `--__init__.py

|-- public

|   |--bg.png

|   |--favicon.ico

|   |--index.html

|   `--pylons-logo.gif

|-- templates

|-- tests

|   |--__init__.py

|   |--functional

|  |   `-- __init__.py

|   `--test_models.py

`-- websetup.py

这个目录里面比较重要的文件和目录有:config/routing.py controllers子目录,model子目录,template子目录,public子目录。

关于routing.py这个文件,主要是一个映射的功能,将URL请求映射到处理请求的方法。(A Python library called Routes handles mapping URLs tocontrollers and their methods, or their action as Routes refers to them)

一般格式为:

map.connect('/{controller}/{action}')

或者

map.connect('/{controller}/{action}/{id}')

例如routing.py中的这一行:

map.connect('/save-template',controller='Notifylist',action='SaveTemplate')

这句话就是前端发来的save-template请求,处理这个请求的就是Notifylist 这个Controller中的SaveTemplate这个函数。

关于controllers这个子目录,包含controllers文件。比如上面的Notifylist.py这个文件。我们来看一下Notifylist.py中的一个代表性的函数def index(self):

  def index(self):
       #return a rendered template
       return render('/helloworld.mako')
       #or, return a string
       #return "hello world"

在routing.py中有这个一行:

map.connect('/index',controller='Notifylist',action='index')


你就应该明白了当浏览器请求index的时候,通过routing这个路由,就转到index这个函数来处理,返回helloworld.mako这个文件。

关于model这个子目录,包含各个数据库表文件,比如有个person表,创建一个person.py模块。person.py文件的内容大致如下,Person类中一般有两个函数:__init__和__repr__。如果有其他的表类内容也相差不大。

 

"""Person model"""
from sqlalchemy import Column
from sqlalchemy.types import Integer, String
from myapp.model.meta import Base
 
class Person(Base):
    __tablename__= "person"
    id = Column(Integer, primary_key=True)
    name = Column(String(100))
    email = Column(String(100))
    def __init__(self, name=’’, email=’’):
        self.name = name
        self.email = email
    def __repr__(self):
        return "<Person(’%s’)" % self.name

关于 public 这个子目录 ,主要是一些静态的文件,如html,js,css,png等等。

关于templates这个子目录,主要是mako文件,mako文件和平时的html文件一样。

5、 到这里pylons的初级篇已经讲得差不多了。

6、 下面讲讲在mako文件中进行数据库请求的过程吧。以保存到数据库为例。

1.  button请求:

保存请求一般会来自html中的save button,例如这个id为btn_save_schemaid_info的button:

<button id= "btn_save_schemaid_info"type="button">保存</button>

2.  在javascript中就会有处理这个button的点击的函数:

$(btn_save_schemaid_info).click(

...
$.ajax('/save-schema-info-by-id?id=' +schema_id_btn
           + '&type=' + schema_type_btn,{
           'success':function(text){
           alert("success:" + text);
           },
           'error':function(text){
           alert("error:" + text);
            }})
...
);

在这个处理btn_save_schemaid_info的函数中,产生了一个ajax请求,save-schema-info-by-id,这个ajax请求有两个参数:id和type,都带有相应的值。

3.  再看看routing中的这个map:

map.connect('/save-schema-info-by-id',controller='Notifylist',action='SaveSchemaInfoById')

在routing中,这个请求又转给了Notifylist这个controller中的SaveSchemaInfoById这个函数。

4.  Notifylist这个controller中的SaveSchemaInfoById函数

def SaveSchemaInfoById(self):
       if "id" in request.params:
            id = int(request.params['id'])
       if "type" in request.params:
            type = int(request.params['type'])
       if id > 0:
            a = self.tablename.filter_by(id =id).first()
            if a:
                a.type = type
                Session.commit()
                return "保存成功"
            else:
                return "此id不存在"
       return "参数错误."

这样就保存到数据库里面了。从这个请求的全过程我们就可以看到pylons架构中MVC各个模块的明显了。多么优越啊~~~

7、 关于json数据格式特殊字符问题.

1>  处理方法就是在发送ajax请求时,对含有特殊格式的字符序列进行两次编码:

encodeURIComponent(encodeURIComponent(data))

2>  如果里面含有中文,需要再在Controller里面:

urllib2.unquote(str(request.params['data']))

3>  读出来的时候controller再进行逆操作:

urllib2.quote(a.template.encode("utf8"))

4>  最后解码:

decodeURIComponent(data)



以上简单介绍了Pylon的各个部分 (setup and configuration, routing, models, controllers, 和 templates) ,希望你已经对 Pylons 的强大有所认识, 你会发现用 Pylons 创建复杂的应用程序是多么的容易 !


原文地址:https://www.cnblogs.com/keanuyaoo/p/3257928.html