class PaginationMiddleware(object): """ Inserts a variable representing the current page onto the request object if it exists in either **GET** or **POST** portions of the request. """ def process_request(self, request): try: request.page = int(request.REQUEST['page']) except (KeyError, ValueError): request.page = 1 def process_view(self, request, view_func, view_args, view_kwargs): if 'paginate' in view_kwargs: del view_kwargs['paginate'] if 'page_url' in view_kwargs: request.page_url = view_kwargs['page_url'] del view_kwargs['page_url'] if 'page' in view_kwargs: request.page = int(view_kwargs['page']) del view_kwargs['page'] else: request.page = 1