一、添加代码

在api文件夹下面新建一个product.py文件,加入以下获取产品列表接口代码

#!/usr/bin/evn python

# coding=utf-8

import json

from bottle import get, put, post, delete

from common import web_helper, db_helper, convert_helper, json_helper, string_helper

@get('/api/product/')

def callback():

"""

获取列表数据

"""

# 设置查询条件

wheres = ''

# 产品分类id

product_class_id = convert_helper.to_int0(web_helper.get_query('product_class_id', '', is_check_null=False))

if product_class_id > 0:

wheres = 'where product_class_id=' + str(product_class_id)

# 页面索引

page_number = convert_helper.to_int1(web_helper.get_query('page', '', is_check_null=False))

# 页面显示记录数量

page_size = convert_helper.to_int0(web_helper.get_query('rows', '', is_check_null=False))

# 排序字段

sidx = web_helper.get_query('sidx', '', is_check_null=False)

# 顺序还是倒序排序

sord = web_helper.get_query('sord', '', is_check_null=False)

# 初始化排序字段

order_by = 'id desc'

if sidx:

order_by = sidx + ' ' + sord

#############################################################

# 初始化输出格式(前端使用jqgrid列表,需要指定输出格式)

data = {

'records': 0,

'total': 0,

'page': 1,

'rows': [],

}

#############################################################

# 执行sql,获取指定条件的记录总数量

sql = 'select count(1) as records from product %(wheres)s' % {'wheres': wheres}

result = db_helper.read(sql)

# 如果查询失败或不存在指定条件记录,则直接返回初始值

if not result or result[0]['records'] == 0:

return data

# 保存总记录数量

data['records'] = result[0].get('records', 0)

#############################################################

### 设置分页索引与页面大小 ###

# 设置分页大小

if page_size is None or page_size <= 0:

page_size = 10

# 计算总页数

if data['records'] % page_size == 0:

page_total = data['records'] // page_size

else:

page_total = data['records'] // page_size + 1

# 记录总页面数量

data['total'] = page_total

# 判断提交的页码是否超出范围

if page_number < 1 or page_number > page_total:

page_number = page_total

# 记录当前页面索引值

data['page'] = page_number

# 计算当前页面要显示的记录起始位置

record_number = (page_number - 1) * page_size

# 设置查询分页条件

paging = ' limit ' + str(page_size) + ' offset ' + str(record_number)

### 设置排序 ###

if not order_by:

order_by = 'id desc'

#############################################################

# 组合sql查询语句

sql = "select * from product %(wheres)s order by %(orderby)s %(paging)s" % \

{'wheres': wheres, 'orderby': order_by, 'paging': paging}

# 读取记录

result = db_helper.read(sql)

if result:

# 存储记录

data['rows'] = result

if data:

# 直接输出json

return web_helper.return_raise(json.dumps(data, cls=json_helper.CJsonEncoder))

else:

return web_helper.return_msg(-1, "查询失败")

二、获取指定id的记录实体

@get('/api/product//')

def callback(id):

"""

获取指定记录

"""

sql = """select * from product where id = %s""" % (id,)

# 读取记录

result = db_helper.read(sql)

if result:

# 直接输出json

return web_helper.return_msg(0, '成功', result[0])

else:

return web_helper.return_msg(-1, "查询失败")

三、添加产品与修改产品接口

@post('/api/product/')

def callback():

"""

新增记录

"""

name = web_helper.get_form('name', '产品名称')

code = web_helper.get_form('code', '产品编码')

product_class_id = convert_helper.to_int0(web_helper.get_form('product_class_id', '产品分类'))

standard = web_helper.get_form('standard', '产品规格')

quality_guarantee_period = web_helper.get_form('quality_guarantee_period', '保质期')

place_of_origin = web_helper.get_form('place_of_origin', '产地')

front_cover_img = web_helper.get_form('front_cover_img', '封面图片')

content = web_helper.get_form('content', '产品描述', is_check_special_char=False)

# 防sql注入攻击处理

content = string_helper.filter_str(content, "'")

# 防xss攻击处理

content = string_helper.clear_xss(content)

is_enable = convert_helper.to_int0(web_helper.get_form('is_enable', '是否启用'))

# 添加记录(使用returning这个函数能返回指定的字段值,这里要求返回新添加记录的自增id值)

sql = """insert into product (name, code, product_class_id, standard, quality_guarantee_period,

place_of_origin, front_cover_img, content, is_enable)

values (%s, %s, %s, %s, %s, %s, %s, %s, %s) returning id"""

vars = (name, code, product_class_id, standard, quality_guarantee_period, place_of_origin, front_cover_img, content, is_enable)

# 写入数据库

result = db_helper.write(sql, vars)

# 判断是否提交成功

if result and result[0].get('id'):

return web_helper.return_msg(0, '成功')

else:

return web_helper.return_msg(-1, "提交失败")

@put('/api/product//')

def callback(id):

"""

修改记录

"""

name = web_helper.get_form('name', '产品名称')

code = web_helper.get_form('code', '产品编码')

product_class_id = convert_helper.to_int0(web_helper.get_form('product_class_id', '产品分类'))

standard = web_helper.get_form('standard', '产品规格')

quality_guarantee_period = web_helper.get_form('quality_guarantee_period', '保质期')

place_of_origin = web_helper.get_form('place_of_origin', '产地')

front_cover_img = web_helper.get_form('front_cover_img', '封面图片')

content = web_helper.get_form('content', '产品描述', is_check_special_char=False)

# 防sql注入攻击处理

content = string_helper.filter_str(content, "'")

# 防xss攻击处理

content = string_helper.clear_xss(content)

is_enable = convert_helper.to_int0(web_helper.get_form('is_enable', '是否启用'))

# 编辑记录

sql = """

update product

set name=%s, code=%s, product_class_id=%s, standard=%s, quality_guarantee_period=%s,

place_of_origin=%s, front_cover_img=%s, content=%s, is_enable=%s

where id=%s returning id"""

vars = (name, code, product_class_id, standard, quality_guarantee_period, place_of_origin, front_cover_img, content,

is_enable, id)

# 写入数据库

result = db_helper.write(sql, vars)

# 判断是否提交成功

if result and result[0].get('id'):

return web_helper.return_msg(0, '成功')

else:

return web_helper.return_msg(-1, "提交失败")

四、删除记录接口

@delete('/api/product//')

def callback(id):

"""

删除指定记录

"""

# 编辑记录

sql = """delete from product where id=%s returning id"""

vars = (id,)

# 写入数据库

result = db_helper.write(sql, vars)

# 判断是否提交成功

if result:

return web_helper.return_msg(0, '成功')

else:

return web_helper.return_msg(-1, "删除失败")

五、修改product_class_edit.html文件

六、 测试代码

python framework jdon_一天学会Python Web框架(十二)产品管理相关推荐

  1. 零基础可以学python吗-零基础适合学Python吗?小白能否学会Python?

    原标题:零基础适合学Python吗?小白能否学会Python? 我没学过编程,能否学会Python? 肯定的回答您:完全能学会 其实,很多个程序员都是从不会到会,每种知识也是从基础到复杂,大家都是从零 ...

  2. 零基础适合学python吗-零基础适合学Python吗?小白能否学会Python?

    原标题:零基础适合学Python吗?小白能否学会Python? 我没学过编程,能否学会Python? 肯定的回答您:完全能学会 其实,很多个程序员都是从不会到会,每种知识也是从基础到复杂,大家都是从零 ...

  3. 0基础小白学python好学吗-零基础适合学Python吗?小白能否学会Python?

    原标题:零基础适合学Python吗?小白能否学会Python? 我没学过编程,能否学会Python? 肯定的回答您:完全能学会 其实,很多个程序员都是从不会到会,每种知识也是从基础到复杂,大家都是从零 ...

  4. python元组添加元素_2分钟学会Python的元组使用

    对于Python 元组,大家了解多少呢?今天我就带大家一起来学习一下Python的元组,让大家快速认识Python的元组,并学会Python的元组在编程中该怎么使用.我们都知道Python的元组与列表 ...

  5. 零基础可以学python吗-零基础可以学会python吗?python好学吗?

    一.编程零基础可以学会python吗? 首先我要说Python是一种解释型语言:这意味着开发过程中没有了编译这个环节.类似于PHP和Perl语言. Python是交互式语言:这意味着,您可以在一个Py ...

  6. 自学python能干些什么副业-学会python能干嘛 学会python可以做哪些兼职?

    学会python可以干什么都希望一段感情会有结果,谁都不希望美好的爱情最后是一场痛,但是如果一开始就想着不在乎天长地久,只在乎曾经拥有"的态度,再美好的感情也不会有好的结果. 从入门级选手到 ...

  7. 学会python做什么兼职_学会python能干嘛 学会python可以做哪些兼职?

    学会python可以干什么都希望一段感情会有结果,谁都不希望美好的爱情最后是一场痛,但是如果一开始就想着不在乎天长地久,只在乎曾经拥有"的态度,再美好的感情也不会有好的结果. 从入门级选手到 ...

  8. 学好python工资一般多少钱-学会Python后,月薪40k是什么水平?

    原标题:学会Python后,月薪40k是什么水平? 现在的职场竞争越来越激烈,不学上一两门新技能,保持自己知识更新,很容易被年轻后辈超越.有些人选择学一门外语,有些人选择学习职场上为人处事的能力. 如 ...

  9. 一张图学会python 3_一张图学会Python?想啥呢?!

    网上有这样一张图片,信息量很大,通常会被配上标题"一张图带你学会Python" 这张图流传甚广,但我没有找到明确的出处,图片上附带了 UliPad 的作者 Limodou 的信息, ...

最新文章

  1. eclipse 无法使用注解的两个解决方法
  2. zend studio自动添加文件注释和方法注释
  3. gis属性表怎么导成excel_使用Python脚本将Excel表批量赋值到ArcGIS属性表
  4. PMCAFF 微课堂 | 赶集、暴风影音这些优质App为什么都在做积分商城?
  5. oracle cpu 利用率过高 kswapd0_服务器带宽监测与利用率过高的解决办法
  6. MongoDB架构图解
  7. Core-3399-JD4 六核高性能AI核心板
  8. P3226-[HNOI2012]集合选数【状压dp】
  9. 人生永无止境的意思是什么_励志人生:生活不会给任何脆弱鼓掌。
  10. linux之trap命令
  11. c++循环执行一个函数_javascript的五种循环,作为程序员,要根据场景和性能作出选择
  12. MSDN WebCast网站全新改版
  13. JavaScript中的严格模式
  14. [猜你喜欢]冠军 yes,boy! 分享 | 推荐系统也可以很简单 做个记录 以后方便学习
  15. 【老生谈算法】matlab实现小波分析源码——小波分析
  16. 2022 年 8 款最佳待办事项列表应用程序
  17. linux下 oracle怎么导入dmp文件
  18. 信息学奥赛一本通:1041:奇偶数判断
  19. Scala中特质的使用以及特质冲突
  20. Redis工具类封装RedisUtils

热门文章

  1. 第十六届全国大学生智能汽车竞赛航天智慧物流总决赛暨第二届“航天杯”移动机器人AI创新技术挑战赛圆满举办
  2. 2021年春季学期-信号与系统-第十次作业参考答案-第三小题
  3. CSDN线上直播操作测试方案
  4. 2020人工神经网络第一次作业-参考答案第四部分
  5. 高压小功率三极管 MFV13001
  6. RDA5807 FM收音机模块
  7. and or not 优先级_我的侠客公测成就奖励大全 成就解锁优先级排名
  8. python网页模板_python-找不到HTML模板
  9. 使用tab键分割的文章能快速转换成表格。( )_无需按空格键,就能将Word文字对齐,3种方法了解一下...
  10. TCP UDP 协议深度解析 (未完待续)