web.py简单demo

1.准备工作

安装python/安装web.py/安装sql插件

2.python代码

  1 # coding:utf-8
  2 import sys 
  3 default_encoding = 'utf-8' 
  4 if sys.getdefaultencoding() != default_encoding: 
  5     reload(sys) 
  6     sys.setdefaultencoding(default_encoding) 
  7 import web
  8 from web import form
  9 import json
 10 
 11 render = web.template.render('templates')
 12 
 13 db = web.database(dbn='mysql', db='last2km', 
 14     host='rds3uiru3je6rmipublic.mysql.rds.aliyuncs.com', user='last2km', pw='c42hn7rLq')
 15 
 16 order_form = form.Form(
 17     form.Textbox("consignee", description="consignee"),
 18     form.Textbox("address", description="address"),
 19     form.Textbox("mobile", description="mobile"),
 20     form.Textbox("receive_amount", description="receive_amount"),
 21     form.Textbox("remark", description="remark"),
 22     form.Button("submit", type="submit", description="Register")
 23 )
 24 
 25 detail_form = form.Form(
 26     form.Textbox("name", description="name"),
 27     form.Textbox("price", description="price"),
 28     form.Textbox("count", description="count"),
 29     form.Button("submit", type="submit", description="submit")
 30 )
 31 
 32 urls = (
 33     '/','index',
 34     '/orderIndex','orderIndex',
 35     '/addOrder','addOrder',
 36     '/updateOrder/(.+)','updateOrder',
 37     '/addOrderDetail/(.+)','addOrderDetail',
 38     '/OrderService/(.+)/(.+)','OrderService'
 39 )
 40 
 41 class index:
 42     def GET(self):
 43         return render.index()
 44 
 45 
 46 class updateOrder:
 47     def GET(self, id):
 48         oser = OrderService()
 49         oser.update(id,1)
 50         return render.orderIndex(oser.getByStatus('0,1,4,16,64'))
 51 
 52 
 53 class orderIndex:
 54     def GET(self):
 55         oser = OrderService()
 56         return render.orderIndex(oser.getByStatus('0,1,4,16,64'))
 57         
 58 
 59 class addOrder:
 60     def GET(self):
 61         f = order_form()
 62         return render.addOrder(f)
 63 
 64     def POST(self):
 65         f = web.input()
 66         a = db.insert('lk_order',seller_id=1, mobile=f['mobile'], 
 67             consignee=f['consignee'],address=f['address'],created_time=web.SQLLiteral("NOW()"))
 68         oser = OrderService()
 69         order = oser.getById(a)
 70         return render.addOrderDetail(detail_form(), order, oser.getDetails(order.id))
 71 
 72 class addOrderDetail():
 73     def GET(self,id):
 74         oser = OrderService()
 75         return render.addOrderDetail(detail_form(), oser.getById(id), oser.getDetails(id))
 76 
 77     def POST(self,id):
 78         f = web.input()
 79         oser = OrderService()
 80         db.insert('lk_order_detail', order_id=id, name=f['name'], price=f['price'], count=f['count'])
 81         return render.addOrderDetail(detail_form(), oser.getById(id), oser.getDetails(id))
 82 
 83 
 84 class OrderService:
 85     def GET(self, Method, data):
 86         web.header('Access-Control-Allow-Origin', '*')
 87         oser = OrderService()
 88 
 89         if Method == 'GET':
 90             order = oser.getById(data)
 91             for k in order.keys():
 92                 order[k] = str(order[k])
 93             oser.generate(order)
 94             return json.dumps(dict(order),ensure_ascii= False)
 95         elif Method == 'STATUS':
 96             orders = []
 97             for order in oser.getByStatus(data):
 98                 for k in order.keys():
 99                     order[k] = str(order[k])
100                 oser.generate(order)
101                 orders.append(dict(order))
102             return json.dumps(orders,ensure_ascii= False)
103         elif Method == 'UPDATE':
104             print data
105             obj = json.loads(data)
106             oser.update(obj['id'], obj['status'])
107             order = oser.getById(obj['id'])
108             for k in order.keys():
109                 order[k] = str(order[k])
110             oser.generate(order)
111             return json.dumps(dict(order),ensure_ascii= False)
112         elif Method == 'DETAIL':
113             orderDetails = []
114             for detail in oser.getDetails(data):
115                 for k in detail.keys():
116                     detail[k] = str(detail[k])
117                 orderDetails.append(dict(detail))
118             return json.dumps(orderDetails,ensure_ascii= False)
119         elif Method == 'ADD':
120             pass
121 
122     def getById(self, id):
123         return db.select('lk_order', where='id=$id', vars=locals())[0]
124 
125     def getByStatus(self, data):
126         status = data.split(',')
127         return db.select('lk_order', where='status in $status', vars=locals())
128 
129     def update(self, id, status):
130         db.update('lk_order', where='id=$id', status=status, vars=locals())
131 
132     def getDetails(self, id):
133         return db.select('lk_order_detail', where='order_id=$id', vars=locals())
134 
135     def getSellerId(self, id):
136         return db.select('lk_seller', where='id=$id', vars=locals())[0]
137 
138     def Add(self, order, details):
139         id = db.insert('lk_order', seller_id=order['seller_id'], consignee=order['consignee'], address=order['address']
140             , mobile=order['mobile'], remark=order['remark'], order_amount=order['order_amount']
141             , receive_amount=order['receive_amount'], status=order['status'],created_time=web.SQLLiteral("NOW()"))
142 
143         for detail in details:
144             db.insert('lk_order_detail', order_id=id, name=detail['name'], price=detail['price'], count=detail['count'])
145         return id
146 
147     def generate(self, order):
148         if order['status'] == '1':
149             order['nextOperation'] = "接单"
150             order['stateClass'] = 'ion-information-circled color-danger'
151         elif order['status'] == '4':
152             order['nextOperation'] = "签收"
153             order['stateClass'] = 'ion-jet color-active'
154         elif order['status'] == '16':
155             order['nextOperation'] = "回单"
156             order['stateClass'] = 'ion-checkmark-circled color-success'
157 
158         sellerId = order['seller_id']
159         sellerTmp = db.select('lk_seller', where='id=$sellerId', vars=locals())[0]
160         order['sellerName'] = sellerTmp['name']
161         order['sellerLogo'] = sellerTmp['logo']
162 
163 if __name__ == "__main__":
164     app = web.application(urls, globals())
165     app.run()
View Code

3.新建一个templates目录放html文件

 1 $def with(orders)
 2 <h1>Order List</h1>
 3 <a href="/addOrder">新增订单</a>
 4 <table>
 5     <thead>
 6         <tr>
 7             <td>订单ID</td>
 8             <td>收件人</td>
 9             <td>电话</td>
10             <td>订单金额</td>
11             <td>应收金额</td>
12             <td>地址</td>
13             <td>备注</td>
14             <td>当前状态</td>
15             <td>下单</td>
16             <td>加货</td>
17         </tr>
18     </thead>
19 $for order in orders
20     <tr>
21         <td>$:order.id</td>
22         <td>$:order.consignee</td>
23         <td>$:order.mobile</td>
24         <td>$:order.order_amount</td>
25         <td>$:order.receive_amount</td>
26         <td>$:order.address</td>
27         <td>$:order.remark</td>
28         $if order.status==0:
29             <td>初始</td>
30         $elif order.status==1:
31             <td>下单</td>
32         $elif order.status==4:
33             <td>配送</td>
34         $elif order.status==16:
35             <td>签收</td>
36         $else:
37             <td>回单</td>
38 
39         $if order.status==0:
40             <td><a href="/updateOrder/$:order.id">下单</a></td>
41             <td><a href="/addOrderDetail/$:order.id">加货</a></td>
42         $else:
43             <td>下单</td>
44             <td>加货</td>
45     </tr>
46 </table>
orderIndex.html
1 $def with(form)
2 <h1>Add Order</h1>
3 <form method="POST">
4     $:form.render()
5 </form>
addOrder.html
 1 $def with(form,order,orderDetails)
 2 <h1>Order detail List</h1>
 3 <table>
 4     <tbody>
 5         <tr>
 6             <th><label for="name">收件人</label></th>
 7             <td>$:order.consignee</td>
 8         </tr>
 9         <tr>
10             <th><label for="price">电话</label></th>
11             <td>$:order.mobile</td>
12         </tr>
13         <tr>
14             <th><label for="count">订单金额</label></th>
15             <td>$:order.order_amount</td>
16         </tr>
17         <tr>
18             <th><label for="count">地址</label></th>
19             <td>$:order.address</td>
20         </tr>
21         <tr>
22             <th><label for="count">备注</label></th>
23             <td>$:order.remark</td>
24         </tr>
25     </tbody>
26 </table>
27 <hr>
28 <table>
29     <thead>
30         <tr>
31             <td>ID</td>
32             <td>货品名称</td>
33             <td>货品单价</td>
34             <td>货品数量</td>
35         </tr>
36     </thead>
37 $for detail in orderDetails
38     <tr>
39         <td>$:detail.id</td>
40         <td>$:detail.name</td>
41         <td>$:detail.price</td>
42         <td>$:detail.count</td>
43     </tr>
44 </table>
45 <form method="POST" action="/addOrderDetail/$:order.id">
46 <table>
47     <tbody>
48         <tr>
49             <th><label for="name">name</label></th>
50             <td><input type="text" id="name" name="name"></td>
51         </tr>
52         <tr>
53             <th><label for="price">price</label></th>
54             <td><input type="text" id="price" name="price"></td>
55         </tr>
56         <tr>
57             <th><label for="count">count</label></th>
58             <td><input type="text" id="count" name="count"></td>
59         </tr>
60         <tr>
61             <th><label for="submit"></label></th>
62             <td><button type="submit" id="submit" name="submit">submit</button></td>
63         </tr>
64     </tbody>
65 </table>
66 </form>
addOrderDetail

4.目录结构如下:

demo
--code.py
--templates
----orderIndex.html
----addOrder.html
----addOrderDetail.html

5.完事

python code.py # 8080  

  

posted @ 2015-07-29 11:11  夜月风  阅读(1331)  评论(0编辑  收藏  举报