<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jonas Renault on Medium]]></title>
        <description><![CDATA[Stories by Jonas Renault on Medium]]></description>
        <link>https://medium.com/@jonasrenault?source=rss-d72eaa7858ec------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*S_B2V626dKnTL-Ra.jpg</url>
            <title>Stories by Jonas Renault on Medium</title>
            <link>https://medium.com/@jonasrenault?source=rss-d72eaa7858ec------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 03:46:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jonasrenault/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Setting up pagination with AngularJS and Django Rest Framework]]></title>
            <link>https://medium.com/@jonasrenault/setting-up-pagination-with-angularjs-and-django-rest-framework-4acadd4b787a?source=rss-d72eaa7858ec------2</link>
            <guid isPermaLink="false">https://medium.com/p/4acadd4b787a</guid>
            <category><![CDATA[django]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angularjs]]></category>
            <dc:creator><![CDATA[Jonas Renault]]></dc:creator>
            <pubDate>Mon, 07 Mar 2016 11:15:33 GMT</pubDate>
            <atom:updated>2016-03-07T11:15:33.510Z</atom:updated>
            <content:encoded><![CDATA[<p>This is a quick guide on an easy way to setup pagination in a webapp built with AngularJS and Django Rest Framework.</p><p>The scenario is quite simple, let’s say we want to display a list of users in one of our pages and we’d like to paginate that list since it could be quite big.</p><h4>Customize pagination in Django</h4><p>Let’s start with the Django part. When querying for users, our server will return only a limited subset of these users as well as information on the total number of results. We’ll put this extra information in the headers as I think it gives cleaner requests.</p><p>To do this, we create a new pagination class that inherits from PageNumberPagination.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a682e6ea83dee4138bdf1b92d493dc42/href">https://medium.com/media/a682e6ea83dee4138bdf1b92d493dc42/href</a></iframe><p>This is very straightforward. We add 4 headers to the response that contain the page number, the page size, the total results count and the total number of pages. The ‘Access-Control-Expose-Headers’ header is needed to make sure the AngularJs app can access the headers when using CORS.</p><p>We’ll change the project settings to use this new pagination class :</p><pre>REST_FRAMEWORK = {<br>    &#39;DEFAULT_PAGINATION_CLASS&#39;: &#39;my_project.apps.core.pagination.HeaderPagination&#39;,<br>    &#39;PAGE_SIZE&#39;: 50<br>}</pre><p>This will enable pagination for all views that use the Django Rest Framework generics views.</p><h4>Create your resources in Angular</h4><p>Now for the AngularJS part.</p><p>First, we’ll create a UserResource using Angular’s $resource service to point to our API endpoint.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f9bc83b97b6d518a3eb9b4f769b9fe2/href">https://medium.com/media/1f9bc83b97b6d518a3eb9b4f769b9fe2/href</a></iframe><p>This is just a regular resource object which can be quite convenient for CRUD methods but doesn’t support pagination by itself. So we create a UserQueryService which we’ll use to manage queries for multiple users that could be paginated.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a6d464a9904688838be0e260f120f68/href">https://medium.com/media/1a6d464a9904688838be0e260f120f68/href</a></iframe><p>This UserQueryService uses the User resource we’ve just defined above to query for users and catch the response. We extract the pagination information from the response headers and store them, along with the list of users as fields on the UserQueryService. Notice the query method returns a promise so it’s convenient to use in resolve functions.</p><p>Don’t forget to add these two services to your Angular module definition.</p><pre>import { UserResource } from ‘../app/components/user/user.service’;<br>import { UserQueryService } from ‘../app/components/user/user.service’;<br>angular.module(&#39;myApp&#39;, [...])<br>.factory(&#39;User&#39;, UserResource)<br>.service(&#39;Users&#39;, UserQueryService)</pre><h4>Using the services</h4><p>Because the query method returns a promise, it’ll work well as a resolve argument in a state definition for example.</p><pre>resolve: {<br> users: function(Users) {<br>  return Users.query();<br> }<br>}</pre><p>This paginated resource then integrates nicely with ui-bootstrap’s <a href="https://angular-ui.github.io/bootstrap/#/pagination">pagination directive</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d315aae141e43f0628e755728f26e56/href">https://medium.com/media/5d315aae141e43f0628e755728f26e56/href</a></iframe><p>That way, changing the page from the ui will set the page number on the UsersQueryService and trigger a new query.</p><p>Each of the user in the data array being a UserResource, you can call all the CRUD methods on them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4acadd4b787a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>