Best Way to use Django — REST API

Photo Thomas from Unsplash

Before the rise of Single Page Applications, applications would render a full HTML document with CSS on the server using PHP, Java or another language and use JavaScript/JQuery and AJAX requests to provide a specific set of interactions a user can have with the web page. This worked very well with Django’s templating system and static files since Django did all of the work for you, loading files that were needed for a specific URL.

But then came Single Page Applications.

Instead of reloading each page due to an action such as a button click— and making the user wait a little longer — Single Page Applications allow for fast reload speeds between pages with asynchronous HTTP requests, providing a seamless user experience, but with a sacrifice of initial load time. Many have or are in the progress of switching to using Javascript frameworks for their front-end stack, and pretty much splitting the front-end from the back-end. This essentially renders Django templates useless.

In my opinion, the best way to use Django for web applications is to use it to build a REST API and use front-end frameworks — React.js, Angular.js, Ember.js, Vue.js— to render your web page, which can be completely independent of Django.

REST APIs

Essentially, an API is the interface the server/backend provides so that apps can talk to them. A REST API is an API that follows a set of rules called REST(Representational State Transfer) and an API endpoint are certain functions of the interface.

Let’s take Instagram for an example, who published their API to the public. So say you wanted to know information about user 1234567, his/her name, how many followers they have, their bio, etc. Given an ACCESS-TOKEN that you get from Instagram, you make a request to an endpoint:

https://api.instagram.com/v1/users/12345678/?access_token=ACCESS-TOKEN

Instagram web servers will then perform certain functions that include searching through their database to get that user and will then return this text, which is in JSON format:

{
“data”: {
“id”: “1234567”,
“username”: “snoopdogg”,
“full_name”: “Snoop Dogg”,
“profile_picture”: "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg",
“bio”: “This is my bio”,
“website”: “http://snoopdogg.com",
“counts”: {
“media”: 1320,
“follows”: 420,
“followed_by”: 3410
}
}
}

If you want more information such as their followers you would then have to make another request to a different URL to get a list of their followers. There are also ways to add users, add photos, etc. if you provide some data, but it is up to instagram whether they want to allow the public to do so. This is how 3rd party apps are created to track who unfollowed you. In fact, Instagram’s mobile apps call a bunch of these API functions and displays this data in a very sleek and fast way(it’s actually very fascinating in how they make their app fast, Instagram’s CTO talks about it here)

Django REST Framework

One great and common way to implement an API with Django is by using Django REST Framework, a Django package packed with powerful but flexible tools to build a REST API. It allows you serialize your data(translating your model objects to — in this case, text), quickly write views/endpoints and test them, add authentication, and much more.

With this, you will be able to separate your back-end from your front-end, enabling you to expand with other formats like IOS and Android applications without just sticking to just web apps. In addition, your front-end and back-end won’t be integrated in a very tight and complex way, which made debugging a living hell. Even companies like Instagram and Doordash have used Django to implement their API as their back-end service, with Instagram using React.js to develop their web app.

This, in my opinion, is the best way to use Django.

More Resources


If you enjoyed reading it, please leave a clap/comment! I’m pretty new at this and I’d like to get better — any response is welcome :)

Also check out my other posts!