设为首页 加入收藏

TOP

我的第一个python web开发框架(17)——产品管理(三)
2017-12-07 14:22:57 】 浏览:584
Tags:一个 python web 开发 框架 产品管理
', '产品描述', is_check_special_char=False) 48 # 防sql注入攻击处理 49 content = string_helper.filter_str(content, "'") 50 # 防xss攻击处理 51 content = string_helper.clear_xss(content) 52 is_enable = convert_helper.to_int0(web_helper.get_form('is_enable', '是否启用')) 53 54 # 编辑记录 55 sql = """ 56 update product 57 set name=%s, code=%s, product_class_id=%s, standard=%s, quality_guarantee_period=%s, 58 place_of_origin=%s, front_cover_img=%s, content=%s, is_enable=%s 59 where id=%s returning id""" 60 vars = (name, code, product_class_id, standard, quality_guarantee_period, place_of_origin, front_cover_img, content, 61 is_enable, id) 62 # 写入数据库 63 result = db_helper.write(sql, vars) 64 # 判断是否提交成功 65 if result and result[0].get('id'): 66 return web_helper.return_msg(0, '成功') 67 else: 68 return web_helper.return_msg(-1, "提交失败") View Code

  使用非get方式提交时,即使用post、put、delete等方式提交参数时,需要使用web_helper.get_form()函数来接收,这一点大家要注意,不然就会获取不到客户端提交的参数值

    # 添加记录(使用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)

  新增记录时,容易出错的地方是参数中的%s与字段数量不匹配,这里大家要注意一下。另外,在insert语句的后尾最好加上returning id或returning *  返回新增记录值或整个记录实体,方便用来判断是否插入成功,如果返回这些内容的话,比较难判断数据库记录是否添加成功了

    # 编辑记录
    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)

  更新记录时,参数元组中记录要将记录的id值放进来,不然也会出现sql执行异常的问题,这个也是容易出错的地方。

 

  删除记录接口

 1 @delete('/api/product/<id:int>/')
 2 def callback(id):
 3     """
 4     删除指定记录
 5     """
 6     # 编辑记录
 7     sql = """delete from product where id=%s returning id"""
 8     vars = (id,)
 9     # 写入数据库
10     result = db_helper.write(sql, vars)
11     # 判断是否提交成功
12     if result:
13         return web_helper.return_msg(0, '成功')
14     else:
15         return web_helper.return_msg(-1, "删除失败")

  

  前端代码大家自己可以比较一下上一章,看看有什么不同

  非常感谢Sunshine-X 的提醒,在产品编辑页面的js中,添加了保存成功后jqgrid表格刷新代码(本人前端比较菜)

  后面附上完整的项目代码

 

 

  本文对应的源码下载

 

版权声明:本文原创发表于 博客园,作者为 AllEmpty 本文欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。

python开发QQ群:669058475    作者博客:http://www.cnblogs.com/EmptyFS/

 

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

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目