Making a Simple REST API Using Django REST Framework


Django is a great framework for creating Web Apps but with necessity of Web Apps comes necessity of RESTful APIs. Django REST Framework gives us a great amount of flexibility to create powerful APIs using Django.

Let’s Make a Movie API


Let’s create a virtual environment first:

Now install the django and django-rest-framework Python packages.

You can run pip freeze to see you have following packages installed.

Create a Project

Now let’s create a Django Project (Movie API) by running the command below.

If you will see Django creates these files for you automatically.

Now let’s see if everything is working by running the command below.

If everything goes well you will see the following output in your terminal.


Django provides us with a built-in server for testing our applications.Run the server using

Now open the in your browser to see the following Django’s welcome page.

Creating an App

Now we will create a Django App (yes! I know it’s confusing sometimes but in Django terminology apps are separate entities in a Web App take it as a subproject inside your main Web App for e.g login/profile etc).

Run the following command to create an app in Django.

You will see a new folder, API is created with a bunch of other files.


Now for the app API to work we first have to add it to the INSTALLED _APPS list in file.


We will now create a database / tables for our movies and actors in file.

Once you create your models register them in the file so that we can access them from the admin panel (will talk about it in a minute). Go to file and add the following code.

Now run the following command from the terminal

This will create a migration file (named inside the migration folder. It contains some information used by Django through which it creates the database tables for us (normally you would have to use SQL to create tables but Django does everything for us).

After this migrate the tables.

This will actually create tables for you in the database.

Now run the following command.

It creates a Django admin user (just like a WordPress C-Panel admin) through which we will be able to see our tables and their data.

Now open the URL in your browser.

Add your login credentials and you can see indeed that our models are present in the panel and you can add new movies, actors and categories through this interface (make sure you add some because we will need them in future to test our API).


Now as we know that data from or to an API is exchanged using some formats like JSON, XML etc. We will add this functionality in our API.

Go ahead & create file inside the api folder and add the following code.

Setting up Endpoints

Now we will create out API Endpoints though which we will request our data, create a new file inside the API folder and add the following code.

We have defined 3 URLs here :

  1. – Home Page for API
  2. – return all movies in the database.
  3. – return all actors in the database.

Make sure you add the api/ inside the movieapi/ Add the following code inside the movieapi/ file.

Now as you can see in the we were importing some functions from file.

We will now define these functions which will handle the logic for our API.

As you can see we have defined 3 functions which handle our API Endpoints:

  • The function named index() handles the root of our API, it just returns an HTTP response Welcome to Movie API v1.0.
  • In the movies() function we are querying all the movie objects from the database and applying the serializer to it which we made earlier. The same stuff is happening in the actor function as well.
  • @api_view is a python decorator which is used here to specify that only GET request should invoke this method.
  • The class JSONResponse is just used to wrap the database objects into a JSON format.

Now if you will head over to in your browser ,you will see that our API is indeed working and returning data in JSON format.



Related Posts