Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 35

Setting Up DRF

Recommended resources

Hello! Welcome to the seventh module of our Django course. In the previous modules, we built a solid foundation in creating traditional, server-rendered web applications. We concluded by looking at how to secure those applications with Django's powerful authentication and authorization system.

Today, we make an exciting shift in focus. We're moving from server-rendered pages to building REST APIs, a core skill for modern backend development. This aligns directly with your goal of learning both full-stack Django and how to create APIs.

Our learning outcome for this lesson is to install and configure Django REST Framework (DRF) in a project. This is the foundational step that will empower our Django application to serve and consume data as a robust API.

Django REST Framework Installation Title Slide
This image visually represents our goal for today: setting up the framework that will allow our Django backend to function as an API, serving data to various clients like web browsers or mobile apps.

By the end of this lesson, you will have successfully integrated DRF into your project and will be ready to start building API endpoints.

What is Django REST Framework (DRF)?

Before we install anything, it's crucial to understand what DRF is and why we use it. You already know Django can handle HTTP requests and return responses. So why add another library?

DRF is a powerful and flexible toolkit that builds on top of Django. It's not a replacement for Django but an extension specifically designed for building Web APIs. It simplifies many common tasks, such as:

  • Serialization: Converting complex data types, like Django model instances, into formats like JSON.
  • Authentication & Permissions: Providing API-specific authentication methods (like token-based authentication) and fine-grained permission controls.
  • Browsable API: Automatically generating a user-friendly HTML interface for your API, which is invaluable for development and testing.

The following video provides an excellent explanation of what DRF is and how it complements Django.

Setting up Django REST Framework DRF - Free Full-stack Course 🦄

Watch this short segment from Vandad Nahavandipoor to understand the distinction between Django and DRF.

Watch from 00:33 to 03:37. Pay attention to the explanation that Django is for general web applications, while DRF is a specialized toolkit for building REST APIs on top of Django.


Step 1: Installation

With a clear understanding of DRF's purpose, let's get it installed. As with any Python project, we will manage our dependencies within a virtual environment. Make sure your project's virtual environment is activated before proceeding.

The installation process involves using pip to install a few packages. The main package is djangorestframework, but we'll also install markdown and django-filter, which are highly recommended dependencies that enable features in DRF's browsable API.

The video below walks through the complete setup process, but we'll focus on the installation commands first.

Setting up Django REST Framework DRF - Free Full-stack Course 🦄

Now, let's watch the installation portion of the video. The instructor will install the necessary packages using pip.

Watch from 09:00 to 12:15. Follow along and run these commands in your own terminal (with your virtual environment activated):

In your terminal, run the following commands:

# Install the core framework
pip install djangorestframework

# Install packages for markdown support in the browsable API
pip install markdown

# Install for filtering support in the browsable API
pip install django-filter

Step 2: Add DRF to INSTALLED_APPS

Just like any other Django app, DRF needs to be registered with our project. We do this by adding it to the INSTALLED_APPS list in our project's settings.py file. This tells Django to recognize DRF and make its features available.

The following video segment explains this step and provides a good refresher on the purpose of the INSTALLED_APPS setting.

Setting up Django REST Framework DRF - Free Full-stack Course 🦄

The next step is to configure our project settings. Watch how to add 'rest_framework' to your INSTALLED_APPS.

Watch from 12:15 to 14:34. Follow the instruction and open your settings.py file and add 'rest_framework' to the INSTALLED_APPS list. It's common practice to add third-party apps below Django's built-in apps.

Your settings.py should look something like this:

# settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Third-party apps
    'rest_framework',

    # Your local apps
    # 'my_app.apps.MyAppConfig',
]
Test your understanding!

You've just added 'rest_framework' to INSTALLED_APPS. Based on your knowledge of Django's architecture, what does this step enable? Why is it necessary?

Show answer

Adding an app to INSTALLED_APPS registers it with the Django project. This allows Django to discover and use the app's components, such as its models, views, templates, and management commands. By adding 'rest_framework', we are telling Django to load the Django REST Framework, making all of its classes (like Serializer, APIView), template tags, and other utilities available for our project to use. Without this step, a pip install alone would do nothing; Django would be completely unaware of DRF's existence.


Step 3: Configure Project URLs for the Browsable API

One of the best features of DRF is its browsable API, which provides a web-based interface to view and interact with your API. To enable the login and logout functionality for this interface, we need to add some URL patterns provided by DRF to our project's main urls.py file.

This process will use the include() function, which you should be familiar with from when we organized URLs for our own apps in Module 2.

Setting up Django REST Framework DRF - Free Full-stack Course 🦄

Let's add the URLs for the browsable API's authentication views. This is an optional but highly recommended step for development.

Watch from 14:34 to 18:25. Follow the instructor to modify your project's main urls.py file. You'll need to import include and add a new path for the DRF authentication routes.

Open your project's root urls.py file (e.g., myproject/urls.py) and add the path for DRF's auth URLs.

# myproject/urls.py
from django.contrib import admin
from django.urls import path, include # Make sure to import include

urlpatterns = [
    path('admin/', admin.site.urls),
    # Add this line
    path('api-auth/', include('rest_framework.urls')),
]

The path 'api-auth/' can be customized, but this is a common convention. This tells Django that any request to /api-auth/... should be handled by the URL patterns defined within Django REST Framework.


Step 4: Verify the Installation

We're done with the setup! The final step is to run the development server and verify that everything is working correctly. A successful installation will be evident when you can access the DRF login page.

Setting up Django REST Framework DRF - Free Full-stack Course 🦄

It's time to check our work. Let's run the server and see the result of our configuration.

Watch from 18:25 to 21:12. Start your development server with python manage.py runserver. Then, open your browser and navigate to the URL you just configured, for example, http://127.0.0.1:8000/api-auth/login/.

If you see a page titled "Django REST framework" with a login form, congratulations! You have successfully installed and configured DRF in your project. You won't be able to log in yet because we haven't created any API-accessible users, but the presence of this page confirms that the framework is correctly integrated.

For a quick textual reference of the core installation steps, you can also consult the official DRF tutorial.

Tutorial 1: Serialization

The official DRF documentation provides a concise, text-based guide to installation that reinforces what we've just done.

Quickly scan the sections 'Setting up a new environment' and 'Getting started'. Notice the pip install djangorestframework command and the addition of 'rest_framework' to INSTALLED_APPS. This serves as a great textual summary of our main steps.



Conclusion

In this lesson, we made the pivotal transition from traditional Django development to the world of APIs. You successfully integrated Django REST Framework, the industry-standard tool for this task.

Key Takeaways:

  • Django REST Framework (DRF) is a specialized toolkit that builds on top of Django to simplify the creation of REST APIs.
  • Installation requires installing the djangorestframework package via pip.
  • Configuration is a two-step process:
    1. Add 'rest_framework' to the INSTALLED_APPS list in settings.py.
    2. Optionally, include rest_framework.urls in your project's urls.py to enable the browsable API's login features.
  • Verification is done by running the server and navigating to the DRF login URL.

Preview of the Next Lesson:
With DRF installed, we are now ready to tackle its most fundamental concept: Serialization. In the next lesson, you will learn what serializers are and create your first Serializer to convert simple data types into JSON, the language of web APIs.