How To Install Django REST Framework (2 Methods)

This post will show you 2 methods to install the Django REST framework in a Django project.

Letโ€™s get started.

Table of Contents

Before You install Django REST Framework.

Before you install the Django REST framework. You have to make sure youโ€™ve got the following prerequisites installed on your computer:

  1. Python: The programming language on which the Django web dev framework is built.
  2. The Django framework itself. Check out how to install Django on Windows for more information. It should be installed either globally or in a virtual environment.
  3. If Django is installed in a virtual environment, make sure the virtual environment is activated.
  4. Youโ€™ve already created a Django project.

The above 3 steps are the ones you have to follow before you can install the Django REST framework. Iโ€™ll start at step 3 as Iโ€™ve already covered the first 2 in other posts. Make sure you follow the links provided if you want to learn more about the steps.

Letโ€™s get moving.

How To Create Django Project.

To create a Django project, first, create a virtual environment using virtualenv or pipnenv or any other virtual environment creator in a favorable folder. Activate the virtual environment and install Django. Then after that, run the following command in your project console: django-admin startproject ProjectName ..

Here is an example:

(env) $ django-admin startproject Config .

It is not a must, but a best practice to use the word Config for your Django project folder. This is because it simply works as a configuration folder for your project. Among other things, it contains the settings.py file which we will use to register the Django REST framework as an installed app.

Also note that there is space then period next to the project folder name (Config). Again, this is not a must, but it is done to create the Django project folder that contains the manage.py file as an immediate child of our project folder. That is, the folder with the virtual environment files or folders.

Thatโ€™s it, now letโ€™s see how we can install the Django REST Framework in the Django folder.

1. How To Install Django REST Framework.

There are two recommended methods to install the Django REST framework in your Django project. One is by using pip and the other is by cloning django-rest-framework from Github. Letโ€™s start with the latter.

Method 1: How To Install Django REST Framework Using Pip

To install Django Rest Framework using pip, run the following command in your Django project console: pip install djangorestframework. After the installation is complete the REST framework will have been installed and ready to be registered in the settings.py file.

Here is how it may look like:

(env) $ pip install djangorestframework
Collecting djangorestframework
  Using cached djangorestframework-3.13.1-py3-none-any.whl (958 kB)
Requirement already satisfied: django>=2.2 in path\env\lib\site-packages (from djangorestframework) (4.0.5)
Collecting pytz
  Using cached pytz-2022.1-py2.py3-none-any.whl (503 kB)
Requirement already satisfied: asgiref<4,>=3.4.1 in path\env\lib\site-packages (from django>=2.2->djangorestframework) (3.5.2)
Requirement already satisfied: sqlparse>=0.2.2 in path\env\lib\site-packages (from django>=2.2->djangorestframework) (0.4.2)
Requirement already satisfied: tzdata in path\env\lib\site-packages (from django>=2.2->djangorestframework) (2022.1)
Installing collected packages: pytz, djangorestframework
Successfully installed djangorestframework-3.13.1 pytz-2022.1

(env) $

This is the best method to install the Django REST framework. I recommend you to use it always.

Method 2: How To Install Django REST Framework by cloning it from Github.

An alternative method to install the Django REST framework is to clone it from Github.

Run the following command on your console: git clone https://github.com/encode/django-rest-framework.

This method requires you to have git installed on your machine.

(env) $ git clone https://github.com/encode/django-rest-framework
Cloning into 'django-rest-framework'...
remote: Enumerating objects: 55495, done.
remote: Counting objects: 100% (147/147), done.
remote: Compressing objects: 100% (87/87), done.
remote: Total 55495 (delta 70), reused 119 (delta 54), pack-reused 55348Receiving objects: 100% (55495/55495), 48.81 MiB | 60.00 KiB/R
Resolving deltas:   0% (0/36979)
Resolving deltas: 100% (36979/36979), done.
Updating files: 100% (603/603), done.

(env) $

I recommend you to use this method if are installing the Django REST framework globally.

2. Add the Django REST framework to Installed Apps.

After the installation of the Django REST framework, itโ€™s now time to add it to the list of installed apps in the settings.py of our project folder.

Below is the code:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',   #new
]

Additional Tips And Tricks on How to Install Django REST Framework.

How To Check The Django REST framework Version Installed.

Checking the version of the Django REST framework installed in your Django is important, you can also use it as a method to check if the REST framework is installed correctly.

The easiest method is to use pip show djangorestframework which shows more information about the REST framework, among those is its version.

(env) $ pip show djangorestframework
Name: djangorestframework
Version: 3.13.1
Summary: Web APIs for Django, made easy.
Home-page: https://www.django-rest-framework.org/
Author: Tom Christie
Author-email: tom@tomchristie.com
License: BSD
Location: path\env\lib\site-packages
Requires: django, pytz
Required-by: 

Check the second line after the command runs.

You can also use pip freeze which lists out all the installed packages in your Django project, among them is the Django REST framework with its version.

(env) $ pip freeze
asgiref==3.5.2
Django==4.0.5
djangorestframework==3.13.1
pytz==2022.1
sqlparse==0.4.2
tzdata==2022.1

Check the third line after the command runs.

If you prefer, the Django shell, Its also a good alternative to check the Django REST framework version.

(env) $ python manage.py shell
>>> import rest_framework
>>> rest_framework.VERSION
'3.13.1'

How To Uninstall Django REST framework.

You can also use pip to uninstall the Django REST framework if you do not want it in your Django project.

(env) $ pip uninstall djangorestframework
...
Proceed (Y/n)? $ y

Conclusion: How To Install Django REST Framework

Congrats, Youโ€™ve just installed and added the Django REST framework to your Django project. Now the next step is to learn the basics of the Django REST framework in order to use it in your project. To learn even more, I recommend William S Vincentโ€™s book Django for APIs. Check my post on the best Django books for beginners to learn more about the book.

How To Install Django REST Framework (2 Methods)

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top