2. skeleton code

 

 


原文: http://webpy.org/skeleton/0.3

 

code.py

import web
import view, config
from view import render

urls = (
    '/', 'index'
)

class index:
    def GET(self):
        return render.base(view.listing())

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.internalerror = web.debugerror
    app.run()

config.py

import web
DB = web.database(dbn='postgres', db='appname', user='username', pw='')
cache = False

db.py

import config

def listing(**k):
    return config.DB.select('items', **k)

view.py

import web
import db
import config

t_globals = dict(
  datestr=web.datestr,
)
render = web.template.render('templates/', cache=config.cache,
    globals=t_globals)
render._keywords['globals']['render'] = render

def listing(**k):
    l = db.listing(**k)
    return render.listing(l)

sql/tables.sql

CREATE TABLE items (
    id serial primary key,
    author_id int references users,
    body text,
    created timestamp default (current_timestamp at time zone 'utc')
);

templates/base.html

$def with (page, title=None)
<html><head>
<title>my site\
$if title: : $title\
</title>
</head><body>
<h1><a href="/">my site</a></h1>
$:page   
</body></html>

templates/listing.html

$def with (items)

$for item in items:
    $:render.item(item)

templates/item.html

$def with (item)

<p>$item.body</p>    
<p class="details">created $datestr(item.created)</p>