分享

[Python]webpy 0.3 skeleton code

 思念是一种饼 2011-04-12

webpy 0.3 skeleton code

Here's the skeleton of a typical 0.3 web.py app:

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>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多