0

下面是属性视图的视图功能,我想给这个添加分页。

类属性视图(资源):

 def get(self):
    try:
        data = request.json
        verifyJwt= request.headers.get('Authorization')
        token = jwt.decode(verifyJwt,"secret",algorithm="HS256")
        print(token.get('email'))        
        data = request.json
        user_email= User.query.filter_by(email=token.get('email')).first()  
        print(user_email,"TEST")
        if user_email != "null" :
            property_data = Property.query.order_by(Property.id.desc()).all()
            property_list = []
            for property_obj in property_data:
                property_list.append({'id': property_obj.id, 'address': property_obj.address, "latitude": property_obj.latitude, "longitude":property_obj.longitude, "cdr_date": property_obj.cdr_date, "building_grossfloor_area": property_obj.building_grossfloor_area, "multi_family": property_obj.multi_family, "artist_studios": property_obj.artist_studios, "max_building_height": property_obj.max_building_height, "stories": property_obj.stories, "retail_commercial_sf": property_obj.retail_commercial_sf, "parking_spaces": property_obj.parking_spaces, "bike_spaces": property_obj.bike_spaces, "architect": property_obj.architect, "developer": property_obj.developer, "site_area": property_obj.site_area, "picture_1": property_obj.picture_1, "picture_2": property_obj.picture_2, "original_name": property_obj.original_name})
        
            response_data = property_list
            return ResponseGenerator(data=response_data,
                                         status=status.HTTP_201_CREATED).make_success_response()

    except Exception as e:
        logger.exception(e)
        return ResponseGenerator(message='Exception: {}'.format(e),
                                 status=status.HTTP_400_BAD_REQUEST).make_error_response()
4

1 回答 1

0

因此,在搜索谷歌并进行如此多的更正之后,我能够以一种简单的方式应用适当的分页,也可以使用搜索过滤器。粘贴以便它可以帮助其他人。

类属性视图(资源):

def post(self):
    try:
            value = data.get('per_page')
            per_page = value
            value1 = data.get('page_num')
            page_num = value1
            search = data.get('search')
            search = "%{}%".format(search)
            property_data = Property.query.order_by(Property.id.desc()).all()
            total = len(property_data)

            page = request.args.get('page', 1, type=int)
            if search !='':
                property_data = Property.query.filter(or_(Property.address.like(search), Property.architect.like(search), Property.developer.like(search))).order_by(Property.id.desc()).paginate(page=page_num, per_page=per_page, error_out=True).items
                num = len(property_data)
                
            else:
                property_data = Property.query.order_by(Property.id.desc()).paginate(page=page_num, per_page=per_page, error_out=True).items
                num = len(property_data)
                
            

            (print("Check Pagination",property_data,page_num,per_page))
            property_list = []
            for property_obj in property_data:
                property_list.append({'id': property_obj.id, 'address': property_obj.address, "latitude": property_obj.latitude, "longitude":property_obj.longitude, "cdr_date": property_obj.cdr_date, "building_grossfloor_area": property_obj.building_grossfloor_area, "multi_family": property_obj.multi_family, "artist_studios": property_obj.artist_studios, "max_building_height": property_obj.max_building_height, "stories": property_obj.stories, "retail_commercial_sf": property_obj.retail_commercial_sf, "parking_spaces": property_obj.parking_spaces, "bike_spaces": property_obj.bike_spaces, "architect": property_obj.architect, "developer": property_obj.developer, "site_area": property_obj.site_area, "picture_1": property_obj.picture_1, "picture_2": property_obj.picture_2, "original_name": property_obj.original_name, "p_status": property_obj.p_status})


        else:
            print("Not authorised to view this data.")
于 2021-04-05T12:16:16.913 回答