[转]web.py在GAE上运行的例子--一个留言本

来源:互联网 发布:企业软件防火墙排名 编辑:程序博客网 时间:2024/05/01 01:02

http://webpy.appspot.com/

# app.yaml

application: webpy
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
script: code.py

 

## code.py

import web
from google.appengine.ext import db

urls = (
  '/', 'index',
  '/note', 'note',
  '/source', 'source',
  '/crash', 'crash'
)

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

class Note(db.Model):
    content = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True)

class index:
    def GET(self):
        notes = db.GqlQuery("SELECT * FROM Note ORDER BY date DESC LIMIT 10")
        return render.index(notes)

class note:
    def POST(self):
        i = web.input('content')
        note = Note()
        note.content = i.content
        note.put()
        return web.seeother('/')

class source:
    def GET(self):
        web.header('Content-Type', 'text/plain')
        return (
          '## code.py/n/n' +
          file('code.py').read() +
          '/n/n## templates/index.html/n/n' +
          file('templates/index.html').read()
        )

class crash:
    def GET(self):
        import logging
        logging.error('test')
        crash

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

## templates/index.html

$def with (notes)
<h1>Hello, world!</h1>

<h2>last 10 notes</h2>

$for note in notes:
    <p>$note.content <span style="color: gray">($note.date)</span></p>

<h2>add a note</h2>
<form method="post" action="/note">
  <textarea name="content"></textarea><br />
<button type="submit">save</button>

<h2>about this site</h2>

<p>This is a web.py example app, demonstrating that it works fine with Google App Engine. <a href="/source">View the source.</a></p>

<address>
  <a href="http://www.aaronsw.com/">Aaron Swartz</a> for <a href="http://webpy.org/">web.py</a>
</address>

原创粉丝点击