Best Way to use Django — REST API
But then came Single Page Applications.
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.
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:
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:
“full_name”: “Snoop Dogg”,
“bio”: “This is my bio”,
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.
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!