Differences between revisions 1 and 7 (spanning 6 versions)
Revision 1 as of 2008-02-24 06:19:46
Size: 329
Editor: ZoomQuiet
Comment:
Revision 7 as of 2009-11-28 20:18:15
Size: 3731
Editor: Elias
Comment: 删除对PageComment2组件的引用
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
http://webpy.org/static/webpy.gif WebPy ~ 超轻量WSGI 应用框架
Line 10: Line 11:
`web.py 0.3 的食谱格式文档.`
Line 14: Line 17:
== Hello World! ==

 命题::
 怎么使用web.py 写个''hello world''?
 解决::
{{{#!python
import web

urls = ("/.*", "hello")
app = web.application(urls, globals())

class hello:
    def GET(self):
        return 'Hello, world!'

if __name__ == "__main__":
    app.run()
}}}

== 客户对话~Sessions ==

 命题::
  如果在web.py 中运用 客户对话?
 解决::
 * `web.session`模块专门进行了支持,以下实例
{{{#!python
import web
urls = (
    "/count", "count",
    "/reset", "reset"
)
app = web.application(urls, locals())
session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'count': 0})

class count:
    def GET(self):
        session.count += 1
        return str(session.count)

class reset:
    def GET(self):
        session.kill()
        return ""

if __name__ == "__main__":
    app.run()
}}}
 * 对话对象在响应请求句柄前加载,前在请求句柄后保存变更
 * `initializer` 参数用以初创对话

== 邮件 ==

 命题::
  如何通过 web.py 操作邮件?
 解决::
  `web.sendmail`函式用以发送邮件
{{{
web.sendmail('cookbook@webpy.org', 'user@example.com', 'subject', 'message')
}}}
 * 配置`web.config`可通过其它邮件服务发送比如:`/usr/lib/sendmail.`
{{{
web.config.smtp_server = 'mail.mydomain.com'
}}}
 * 可以在`to_address` 参数中指定多个地址
{{{
web.sendmail('cookbook@webpy.org'
    , ['user1@example.com', 'user2@example.com']
    , 'subject', 'message')
}}}
 * 使用`cc`和`bcc`键参数,则声明转发和暗送邮箱地址(使用列表,同樣支持多地址Cc Bcc)
{{{
web.sendmail('cookbook@webpy.org',
    'user@example.com', 'subject', 'message',
    cc='user1@example.com',
    bcc='user2@example.com')
}}}

== 网站排版模板 ==

 命题::Problem
  如何在web.py 应用中使用排版模板?
 解决::
  在应用过程中直接使用
{{{#!python

app = web.application(urls, gloabals)
render = web.template.render('templates/')

def layout_processor(handle):
    result = handle()
    return render.layout(result)

app.add_processor(layout_processor)
}}}

== 文件上传 ==
 命题::
   即使你对表单上传或是CGI生成不熟悉,文件上传也可以轻易支持的

 解决::
{{{
import web

urls = ('/upload', 'Upload')

class Upload:
    def GET(self):
        return """<html><head></head><body>
<form method="POST" enctype="multipart/form-data" action="">
<input type="file" name="myfile" />
<br/>
<input type="submit" />
</form>
</body></html>"""

    def POST(self):
        x = web.input(myfile={})
        web.debug(x['myfile'].value) # This is the file contents
        web.debug(x['myfile'].filename) # This is the filename
        web.redirect('/upload')


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

if __name__ == "__main__":
    app.run()
}}}

 探讨::
   有些事儿得注意:
  * 对应表单必须有`enctype="multipart/form-data"`聲明,否则不会起作用
  * 在 webpy 代码中, 如果将文件作为想导入CGI`FieldStorage`对象,默认值是必须的(myfile={}部分) .
  * 否则将被视作文本文件-- 此例中,你这无法知道文件命了.


== 预备命题 ==

    * How to properly use web.background
Line 16: Line 158:
[[PageComment2]] [:/PageCommentData:PageCommentData]

http://webpy.org/static/webpy.gif WebPy ~ 超轻量WSGI 应用框架

::-- ZoomQuiet [DateTime(2008-02-24T06:19:46Z)] TableOfContents

Include(CPUGnav)

web.py 0.3 的食谱格式文档.

1. web.py 食谱

1.1. Hello World!

命题

怎么使用web.py 写个hello world?

解决

Toggle line numbers
   1 import web
   2 
   3 urls = ("/.*", "hello")
   4 app = web.application(urls, globals())
   5 
   6 class hello:
   7     def GET(self):
   8         return 'Hello, world!'
   9 
  10 if __name__ == "__main__":
  11     app.run()

1.2. 客户对话~Sessions

命题
  • 如果在web.py 中运用 客户对话?
解决
  • web.session模块专门进行了支持,以下实例

  • Toggle line numbers
       1 import web
       2 urls = (
       3     "/count", "count",
       4     "/reset", "reset"
       5 )
       6 app = web.application(urls, locals())
       7 session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'count': 0})
       8 
       9 class count:
      10     def GET(self):
      11         session.count += 1
      12         return str(session.count)
      13 
      14 class reset:
      15     def GET(self):
      16         session.kill()
      17         return ""
      18 
      19 if __name__ == "__main__":
      20     app.run()
    
    • 对话对象在响应请求句柄前加载,前在请求句柄后保存变更
    • initializer 参数用以初创对话

    1.3. 邮件

    命题
    • 如何通过 web.py 操作邮件?
    解决
    • web.sendmail函式用以发送邮件

    web.sendmail('cookbook@webpy.org', 'user@example.com', 'subject', 'message')
    • 配置web.config可通过其它邮件服务发送比如:/usr/lib/sendmail.

    web.config.smtp_server = 'mail.mydomain.com'
    • 可以在to_address 参数中指定多个地址

    web.sendmail('cookbook@webpy.org'
        , ['user1@example.com', 'user2@example.com']
        , 'subject', 'message')
    • 使用ccbcc键参数,则声明转发和暗送邮箱地址(使用列表,同樣支持多地址Cc Bcc)

    web.sendmail('cookbook@webpy.org', 
        'user@example.com', 'subject', 'message', 
        cc='user1@example.com', 
        bcc='user2@example.com')

    1.4. 网站排版模板

    • 命题::Problem
      • 如何在web.py 应用中使用排版模板?
    • 解决
      • 在应用过程中直接使用

    Toggle line numbers
       1 app = web.application(urls, gloabals)
       2 render = web.template.render('templates/')
       3 
       4 def layout_processor(handle):
       5     result = handle()
       6     return render.layout(result)
       7 
       8 app.add_processor(layout_processor)
    

    1.5. 文件上传

    命题
    • 即使你对表单上传或是CGI生成不熟悉,文件上传也可以轻易支持的
    解决

    import web
    
    urls = ('/upload', 'Upload')
    
    class Upload:
        def GET(self):
            return """<html><head></head><body>
    <form method="POST" enctype="multipart/form-data" action="">
    <input type="file" name="myfile" />
    <br/>
    <input type="submit" />
    </form>
    </body></html>"""
    
        def POST(self):
            x = web.input(myfile={})
            web.debug(x['myfile'].value) # This is the file contents
            web.debug(x['myfile'].filename) # This is the filename
            web.redirect('/upload')
    
    
    app = web.application(urls, globals())
    
    if __name__ == "__main__":
        app.run()
    探讨
    • 有些事儿得注意:
    • 对应表单必须有enctype="multipart/form-data"聲明,否则不会起作用

    • 在 webpy 代码中, 如果将文件作为想导入CGIFieldStorage对象,默认值是必须的(myfile={}部分) .

    • 否则将被视作文本文件-- 此例中,你这无法知道文件命了.

    1.6. 预备命题

    • How to properly use web.background

    1.7. 反馈

    WebPyCookBook (last edited 2009-12-25 07:15:23 by localhost)