Install Django REST Framework in Your Django Project

This post will show you how 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.

First Create a 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.

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.

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

Adding 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

Alt Method: Install DRF 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.

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

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 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.

Stephen Mclin
Stephen Mclin

Hey, I'm Steve; I write about Python and Django as if I'm teaching myself. CodingGear is sort of like my learning notes, but for all of us. Hope you'll love the content!

Articles: 125

Leave a Reply

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