Python后台分页删除编辑查询

「POST 数据」通常指 POST 时 body 中的数据。
而 QueryString (URL)中也有可以带参数(通常是 GET 时的参数)。如果 POST 时同时存在 QueryString 和 body data,那么 get_argument 获取得到的其实是 QueryString 中的数据,而不是 body 中的数据。
get_body_argument 这个方法是获取 body 中的参数值。
分页和查询:
class AjaxLockListHandler(BaseHandler):
    # @tornado.web.authenticated
    def post(self):
        try:
            req_data = json.loads(self.request.body.decode())
        except Exception as e:
            print("AjaxLockListHandler handle POST request error: %s, it should be the data from client is illegal" % str(e))
            self.write(REQUEST_ERROR)
            return

        print("AjaxLockListHandler receive POST request: %s" % req_data)

        params = {}
        # TODO: 分页和查询参数合法性检查
        # ——分页所需数据——
        # 起始位置,默认为0
        if "start" in req_data:
            params["offset"] = req_data["start"]
        else:
            params["offset"] = 0
        # 每页显示多少数据,默认为10
        if "limit" in req_data:
            params["limit"] = req_data["limit"]
        else:
            params["limit"] = 10
        # dataTable校验用的参数
        if "draw" in req_data:
            draw = req_data["draw"]
        else:
            draw = 0

        # ——查询所需数据——
        # product_ver字段数据,REST需要string类型,可有可无,若没有表示不关心该项数据,下同
        if "product_ver" in req_data:
            params["product_ver"] = req_data["product_ver"]
        # lock_id字段数据,REST需要uint32类型数据,可有可无
        if "lock_id" in req_data:
            params["lock_id"] = int(req_data["lock_id"])
        # firmware_ver字段数据,REST需要string类型数据,可有可无
        if "firmware_ver" in req_data:
            params["firmware_ver"] = req_data["firmware_ver"]
        # corp_id字段数据,REST需要int类型数据,可有可无
        if "corp_id" in req_data:
            params["corp_id"] = int(req_data["corp_id"])
        # master_id字段数据,REST需要int型数据,可有可无
        if "master_id" in req_data:
            params["master_id"] = int(req_data["master_id"])
        # mac字段数据,REST需要string类型数据,可有可无
        if "mac" in req_data:
            params["mac"] = req_data["mac"]
        # cat_eye字段数据,REST需要int型数据,可有可无
        # 【注意】:数据库中该字段为boolean类型,表示有无猫眼,但查询的时候可能不关心该项数据,所以此处定义为int类型
        if "cat_eye" in req_data:
            params["cat_eye"] = int(req_data["cat_eye"])
        # has_ap字段数据,REST需要int型数据,可有可无
        # 【注意】:数据库中该字段为boolean类型,表示有无猫眼,但查询的时候可能不关心该项数据,所以此处定义为int类型
        if "has_ap" in req_data:
            params["has_ap"] = int(req_data["has_ap"])

        p_str = json.dumps(params, separators=(',', ':'), sort_keys=True) + "+" + APP_SECRET
        sign = md5(p_str)
        params["sign"] = sign
        print("AjaxLockListHandler handle POST request,request_body: %s" % params)

        content, err = http.fetch("/admin/lock/list", json.dumps(params))
        if err is not None:
            self.write(SERVER_ERROR)
        elif len(content) == 0:
            table_data = json.dumps({"draw": draw, "recordsTotal": 0, "data": "", "recordsFiltered": 0})
            self.write(table_data)
        else:
            js = json.loads(content.decode())
            if "data" in js:
                data = js["data"]
                table_data = json.dumps({"draw": draw, "recordsTotal": data["total"], "data": data["locks"], "recordsFiltered": data["total"]})
                self.write(table_data)
                # self.render('lock/list.html', data=table_data)
            else:
                self.write(SERVER_ERROR)

删除:

class AjaxLockDeleteHandler(BaseHandler):
    # @tornado.web.authenticated
    def post(self):
        try:
            req_data = json.loads(self.request.body.decode())
        except Exception as e:
            print(
                "AjaxLockDeleteHandler handle POST request error: %s, it should be the data from client is illegal" % str(
                    e))
            self.write(REQUEST_ERROR)
            return

        if "lock_id" in req_data:
            lock_id = int(req_data["lock_id"])
            # lock_id合法性检查
            if lock_id <= 0:
                self.write(REQUEST_ERROR)
                return
            else:
                print("AjaxLockDeleteHandler receive request of lock_id: %s" % lock_id)
                params = {"lock_id": lock_id}
                p_str = json.dumps(params, separators=(',', ':'), sort_keys=True) + "+" + APP_SECRET
                sign = md5(p_str)
                params["sign"] = sign

                content, err = http.fetch("/admin/lock/delete", json.dumps(params))
                print("====", content)
                if err is not None:
                    print("AjaxLockDeleteHandler handle post request to REST server error: %s" % str(err))
                    if err.code == 400:
                        self.write(REQUEST_ERROR)
                    else:
                        self.write(SERVER_ERROR)
                else:
                    js = json.loads(content.decode())
                    if "data" in js:
                        data = js["data"]
                        table_data = json.dumps({"data": data})
                        self.write(table_data)
        else:
            self.write(REQUEST_ERROR)

修改:

class AjaxLockUpdateHandler(BaseHandler):

    def post(self):
        try:
            req_data = json.loads(self.request.body.decode())
        except Exception as e:
            print(
                "AjaxLockUpdateHandler handle POST request error: %s, it should be the data from client is illegal" % str(
                    e))
            self.write(REQUEST_ERROR)
            return

        # 添加descr?
        if "lock_id" in req_data and "name" in req_data:
            if req_data["lock_id"] <= 0 or req_data["name"] == "":
                print("AjaxLockUpdateHandler handle POST request error: lock_id is illegal or name is NULL")
                self.write(REQUEST_ERROR)
            else:
                params = {"lock_id": req_data["lock_id"], "name": req_data["name"]}
                p_str = json.dumps(params, separators=(',', ':'), sort_keys=True) + "+" + APP_SECRET
                sign = md5(p_str)
                params["sign"] = sign
                print(params)
                content, err = http.fetch("/admin/lock/update", json.dumps(params))
                if err is not None:
                    print("AjaxLockUpdateHandler handle post request to REST server error: %s" % str(err))
                    if err.code == 400:
                        self.write(REQUEST_ERROR)
                    else:
                        self.write(SERVER_ERROR)
                else:
                    print("AjaxLockUpdateHandler handle post request to REST server success")
                    self.write("{}")

跟dataTable+分页。。。js是相关联的

转载于:https://www.cnblogs.com/TangYR168/p/6237741.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值