94.(后端)分类参数ID获取接口实现——restful发送请求查询数据

1.概述

这里通过id参数,使用get请求查询数据

2.代码展示

# attribute表
# restful形式发送请求
class Attribute(Resource):
    # 增加数据
    def post(self):
        name = request.form.get('name')
        cid = request.form.get('cid')
        _type = request.form.get('_type')
        val = request.form.get('val')

        if all([name, cid, _type]):
            if val:
                attr = models.Attribute(name=name, cid=int(cid), _type=_type, val=val)
            else:
                attr = models.Attribute(name=name, cid=int(cid), _type=_type)
            db.session.add(attr)
            db.session.commit()
            return to_dict_msg(200,msg="添加商品分类数据成功!!!!")
        else:
            return to_dict_msg(10000)

    # 查询数据
    def get(self):
        try:
            id = request.args.get('id')
            attr = models.Attribute.query.get(id)
            if attr:
                return to_dict_msg(200, attr.to_dict(), msg="获取分类参数成功!!!")
            else:
                return to_dict_msg(10022)
        except Exception as e:
            return to_dict_msg(20000)
attribute_api.add_resource(Attribute, '/attribute')

3.Postman测试接口

94.(后端)分类参数ID获取接口实现——restful发送请求查询数据_第1张图片
94.(后端)分类参数ID获取接口实现——restful发送请求查询数据_第2张图片

你可能感兴趣的:(电子商城管理系统,restful,后端,python,flask)