天天看點

Webpy 0.3新手指南

 一、簡單調用

1.安裝ActivePython-2.7.2.5-win32-x86.msi

2.http://webpy.org下載下傳web.py-0.36.tar.gz,解壓

3.轉到解壓目錄web.py-0.36執行python setup.py install

4.測試代碼

code.py

import web

urls = (

   '/', 'index'

)

class index:

def GET(self):

return "Hello, world!" 

if __name__ == "__main__":

app = web.application(urls, globals())

app.run()

5.運作程式

E:\python\webpy>python code.py

http://0.0.0.0:8080/

6.IE通路

http://localhost:8080,提示“Hello,world!”

二、使用模闆

1.ct1.py

render = web.template.render('templates/')

'/(.*)', 'index'

class index:

def GET(self,name):

return render.index(name)

2.模闆html檔案

程式ct1.py目錄下建立templates目錄,進入templates目錄建立index.html檔案,内容如下:

$def with (name)

$if name:

I just wanted to say <em>hello</em> to $name.

$else:

<em>Hello</em>, world!

3.執行測試

1).進入指令行界面

轉入程式ct1.py目錄下,執行python ct1.py

E:\python\webpy>python ct1.py

2).打開IE界面,輸入“http://localhost:8080/Hellen”

IE輸出"I just wanted to say hello to Hellen."

三、使用資料庫

1.安裝cx_Oracle子產品

首先安裝cx_Oracle子產品,确認可以成功通路資料庫

2.指令行測試

>>> import web

>>> db = web.database(dbn='oracle',user = 'test' ,pw = 'test',db='testtns<遠端服務名tnsnames.ora等号左邊名字>')

>>> rows = db.select('test')

0.0 (3): SELECT * FROM test

>>> for row in rows:

...     print row

...

<Storage {'ID': 1, 'IP': '192.168.1.11                 '}>

<Storage {'ID': 2, 'IP': '192.168.1.12                 '}>

<Storage {'ID': 3, 'IP': '192.168.1.13                '}>

<Storage {'ID': 4, 'IP': '192.168.1.14                 '}>

<Storage {'ID': 5, 'IP': '192.168.1.15                 '}>

<Storage {'ID': 6, 'IP': '192.168.1.16                 '}>

>>>

3.準備工作

1)建立表

CREATE TABLE test (

  id number(20) primary key not null,

  title varchar2(20),

  created timestamp default current_timestamp, --預設值為目前時間戳

  done char(1) check(done in ('N','Y'))  

);

2)修改done列預設值為‘Y’

alter table test MODIFY DONE default 'Y'; -- done列預設值為‘Y’

3)建立序列

CREATE SEQUENCE test_sequence

INCREMENT BY 1

START WITH 1

NOMAXvalue

NOCYCLE

CACHE 10;

3)建立觸發器

保證test表id字段插入新記錄時自動增加 -- id列預設值為‘TEST_SEQUENCE.Nextval’

create or replace trigger tr_user

before insert on test

for each row

begin

select TEST_SEQUENCE.Nextval into :new.id from dual;

end;

結果: 隻保留title一個字段無預設值,使用者插入時允許隻指定一個title字段,其它為預設

4)插入記錄

INSERT INTO test(id,title,done) VALUES (TEST_SEQUENCE.Nextval,'Learn web.py','Y');

INSERT INTO test(id,title,done) VALUES (TEST_SEQUENCE.Nextval,'Python web.py','Y');

insert into test (title) values('Python Web 架構')

5)指令行測試連接配接

>>> db = web.database(dbn='oracle',user = 'test' ,pw = 'test',db='testtns')

0.0 (1): SELECT * FROM test

<Storage {'TITLE': 'Learn web.py', 'DONE': 'Y', 'ID': 2, 'CREATED': datetime.dat

etime(2013, 4, 8, 13, 1, 52, 685605)}>

>>> rows = db.query('select * from test')

0.0 (1): select * from test

...     print row['ID'],row['TITLE']

2 Learn web.py

3 Python web.py

4 Python Web 架構

5 Web.py 架構 

4.服務端程式

cd1.py

db = web.database(dbn='oracle',user = 'test' ,pw = 'test',db='testtns')

'/', 'index'

rows = db.select('test')

return render.index(rows)

5.index.html

程式cd1.py目錄下建立templates目錄,進入templates目錄建立index.html檔案,内容如下:

$def with (rows)

<ul>

$for row in rows:

    <li> $row['TITLE'] </li>

</ul>

打開IE通路“http://localhost:8080/”

Learn web.py

Python web.py

Python Web 架構

Web.py 架構 

本文轉自 pgmia 51CTO部落格,原文連結:http://blog.51cto.com/heyiyi/1173594