设为首页 加入收藏

TOP

我的第一个python web开发框架(17)——产品管理(一)
2017-12-07 14:22:57 】 浏览:579
Tags:一个 python web 开发 框架 产品管理

  这是后台管理系统最后一个功能,产品管理,它的接口与页面功能与上一章差不多。

 

 

  获取产品列表接口

 1 @get('/api/product/')
 2 def callback():
 3     """
 4     获取列表数据
 5     """
 6     # 设置查询条件
 7     wheres = ''
 8     # 产品分类id
 9     product_class_id = convert_helper.to_int0(web_helper.get_query('product_class_id', '', is_check_null=False))
10     if product_class_id > 0:
11         wheres = 'where product_class_id=' + str(product_class_id)
12     # 页面索引
13     page_number = convert_helper.to_int1(web_helper.get_query('page', '', is_check_null=False))
14     # 页面显示记录数量
15     page_size = convert_helper.to_int0(web_helper.get_query('rows', '', is_check_null=False))
16     # 排序字段
17     sidx = web_helper.get_query('sidx', '', is_check_null=False)
18     # 顺序还是倒序排序
19     sord = web_helper.get_query('sord', '', is_check_null=False)
20     # 初始化排序字段
21     order_by = 'id desc'
22     if sidx:
23         order_by = sidx + ' ' + sord
24 
25     #############################################################
26     # 初始化输出格式(前端使用jqgrid列表,需要指定输出格式)
27     data = {
28         'records': 0,
29         'total': 0,
30         'page': 1,
31         'rows': [],
32     }
33     #############################################################
34     # 执行sql,获取指定条件的记录总数量
35     sql = 'select count(1) as records from product %(wheres)s' % {'wheres': wheres}
36     result = db_helper.read(sql)
37     # 如果查询失败或不存在指定条件记录,则直接返回初始值
38     if not result or result[0]['records'] == 0:
39         return data
40     # 保存总记录数量
41     data['records'] = result[0].get('records', 0)
42 
43     #############################################################
44     ### 设置分页索引与页面大小 ###
45     # 设置分页大小
46     if page_size is None or page_size <= 0:
47         page_size = 10
48     # 计算总页数
49     if data['records'] % page_size == 0:
50         page_total = data['records'] // page_size
51     else:
52         page_total = data['records'] // page_size + 1
53     # 记录总页面数量
54     data['total'] = page_total
55 
56     # 判断提交的页码是否超出范围
57     if page_number < 1 or page_number > page_total:
58         page_number = page_total
59     # 记录当前页面索引值
60     data['page'] = page_number
61 
62     # 计算当前页面要显示的记录起始位置
63     record_number = (page_number - 1) * page_size
64     # 设置查询分页条件
65     paging = ' limit ' + str(page_size) + ' offset ' + str(record_number)
66     ### 设置排序 ###
67     if not order_by:
68         order_by = 'id desc'
69     #############################################################
70 
71     # 组合sql查询语句
72     sql = "select * from product %(wheres)s order by %(orderby)s %(paging)s" % \
73            {'wheres': wheres, 'orderby': order_by, 'paging': paging}
74     # 读取记录
75     result = db_helper.read(sql)
76     if result:
77         # 存储记录
78         data['rows'] = result
79 
80     if data:
81         # 直接输出json
82         return web_helper.return_raise(json.dumps(data, cls=json_helper.CJsonEncoder))
83     else:
84         return web_helper.return_msg(-1, "查询失败")
View Code

  这个接口多了按产品分类id查询的条件,如果少了这个的话,直接将产品分类字段替换为产品字段变量就可以了。

  大家可以看到这里的代码好像有点复杂。是的,这里要进行分页查询进行了分页处理,所以代码有点多,不过写了很详细的注释,只要你对python的基本语法、字典的处理理解,然后对之前工具函数那里按要求重写过测试用例,那么对阅读这段代码是没有什么大问题的。

  下面再重新帮大家熟悉一下前面讲述过的工具函数

product_class_id = convert_helper.to_int0(web_helper.get_query('product_class_id', '产品分类id', is_check_null=False))

  这是获取客户端(HTML)用AJAX提交上来的产品分类id接收处理,如果你认真看过前面工具函数内容的话,看到web_helper.get_query()这个函数应该会很熟悉,它就是获取GET方式提交值的接收函数,第一个参数是要接收的变量名称,第二个参数是这个变量的中文说明,第三个是在接收参数时,是否做非空判断,当前设置为这不是必填项。默认它为True,当为True时,如果客户端没有提交这个参数值,则系统自动会返回“xxx 不允许为空”,这个xxx就是第二个参数,也就

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇djang-分页 下一篇Django的分页器(paginator)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目