Hello! Welcome back to our module on building REST APIs with Django.
In our last lesson, we covered the "what" and "why" of REST APIs. You learned that REST is an architectural style that decouples the client (front-end) from the server (back-end), allowing them to communicate using standard HTTP methods. This separation provides incredible flexibility, enabling a single back-end to serve various platforms like web browsers, mobile apps, and more.
Today, we move from theory to practice. Our goal is to install and configure the Django REST Framework (DRF). DRF is a powerful and flexible toolkit that makes building REST APIs inside a Django project incredibly efficient. By the end of this lesson, your Task Manager project will be set up and ready for you to start building your first API endpoint.
Let's get started!
1. Preparing Your Project
Before we install anything new, let's get your development environment ready. We will continue working on the Task Manager project you've built throughout this course.
- Open your project in your code editor (like Visual Studio Code).
- Open a new terminal within your editor.
- Activate your virtual environment. This is a crucial step to ensure the new package we're about to install is kept isolated to this project.
Depending on your operating system and how you set it up, the command will be one of the following:
- macOS/Linux:
source .venv/bin/activate - Windows:
.venv\Scripts\activate
You'll know it's active when you see the name of your virtual environment in parentheses at the start of your terminal prompt, like (.venv).
2. Installing Django REST Framework
With your environment active, installing DRF is a straightforward process using pip, Python's package installer.
To get a quick overview of the installation process, let's watch a short video clip.
Django REST Framework Oversimplified
This clip from the video 'Django REST Framework Oversimplified' by Dennis Ivy gets straight to the point, showing you the exact command to install DRF and the first configuration step.
Watch from 0:53 to 1:10. This part covers the pip install command and the subsequent addition of rest_framework to your settings file.
As shown in the video, run the following command in your activated terminal:
pip install djangorestframework
This command downloads and installs the Django REST Framework package and its dependencies into your project's virtual environment.
3. Configuring Your Django Project
Simply installing the package isn't enough; you also need to tell your Django project to use it. This process is often called "registering" the app.
The following guide provides a clear, step-by-step walkthrough of the entire setup process. We'll use it to guide our configuration.
A Beginner's Guide to Building REST APIs with Python and ...
This article, 'A Beginner's Guide to Building REST APIs with Python and Django REST Framework', provides a concise text-based reference for the steps we're taking.
Please read 'Step 4: Set Up Django REST Framework'. It shows you exactly what to add to your settings.py file.
As the article explains, you need to add 'rest_framework' to the INSTALLED_APPS list in your project's settings.py file.
- Open the
settings.pyfile for your project (e.g.,task_manager/settings.py). - Find the
INSTALLED_APPSlist. - Add
'rest_framework'to the list. It's common practice to add third-party apps below Django's built-in apps and above your own custom apps.
Your INSTALLED_APPS should now look something like this:
# task_manager/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',
# My apps
'tasks',
# ... any other apps you have
]
By adding this line, you make all of DRF's features, such as its special views, serializers, and the browsable API, available to your project.
4. Best Practice: Creating a Dedicated api App
While you could put your API code directly into your existing tasks app, a common and recommended practice is to create a separate app dedicated to the API. This improves the organization of your project by separating the concerns of your traditional web pages from your API endpoints.
In your terminal, run the startapp command:
python manage.py startapp api
This will create a new api directory with the standard Django app structure (models.py, views.py, etc.).
Just like with rest_framework, you must also register this new app. Open your settings.py file again and add 'api' to your INSTALLED_APPS list.
# task_manager/settings.py
INSTALLED_APPS = [
# ... django apps
# Third-party apps
'rest_framework',
# My apps
'tasks',
'api', # Add the new app here
]
Test your understanding!
You've now added both 'rest_framework' and 'api' to your INSTALLED_APPS. A colleague who is new to Django asks you why this step is necessary after you've already created the app directory with startapp. How would you explain it?
Show answer
You could explain that the INSTALLED_APPS list is Django's central registry for all active applications in a project. Running startapp only creates the file structure for an app. By adding its name to INSTALLED_APPS, you are telling Django to actually include that app in its processes. This allows Django to discover the app's models for database migrations, find its URL configurations, locate its templates, and use any other components it provides. Without this step, the app's code exists but is invisible to the rest of the Django project.
5. Verifying the Installation
At this point, Django REST Framework is installed and configured. To confirm that everything is set up correctly, run the development server:
python manage.py runserver
If the server starts up without any import errors or configuration-related crashes, your setup was successful! You won't see any changes to your website yet, but the absence of errors confirms that Django recognizes DRF and your new api app. You can stop the server with Ctrl+C.
Conclusion
Excellent work! You have successfully taken the first crucial, practical step in building a REST API. You've prepared your project, installed a key third-party package, and configured your Django settings to integrate it.
Key Takeaways:
- Always activate your virtual environment before installing new packages.
- Django REST Framework is installed with the command
pip install djangorestframework. - To integrate a new package or app, you must add it to the
INSTALLED_APPSlist in yoursettings.pyfile. - It is a best practice to create a dedicated app (e.g.,
api) to house your API-related code, keeping it separate from your traditional web views.
Preview of the Next Lesson:
Now that the framework is in place, our next task is to define how our data should be represented. In the next lesson, you will learn how to create serializers to convert Django model instances to and from JSON. A serializer is the magic that translates your complex Python objects (like your Task model) into a simple format like JSON that can be easily sent over the internet.