Django is a powerful web framework, but when combined with Django REST Framework (DRF), it becomes a go-to tool for building RESTful APIs. Whether you're developing a backend for a mobile app, a React frontend, or simply exploring API development, DRF streamlines the process with its robust features.
This step-by-step tutorial will set up a Task Manager API using Django and DRF. By the end, you’ll have a fully functional Django project with DRF installed, ready to create API endpoints.
Table of Contents
- Prerequisites
- Step 1: Create a Virtual Environment
- Step 2: Install Django and Django REST Framework
- Step 3: Start a Django Project
- Step 4: Create a Django App for Tasks
- Step 5: Configure Django REST Framework
- Step 6: Create a Simple Model for Tasks
- Conclusion
- What’s Next?
- Summary Checklist
Prerequisites
Before diving into building your Task Manager API with Django REST Framework, ensure you have:
- Python 3.8+ installed (download from python.org if needed)
- Basic knowledge of Python and Django
- Familiarity with the command line
These essentials will help you follow along smoothly.
Step 1: Create a Virtual Environment
Start by setting up a project folder and a virtual environment to keep dependencies isolated:
# Create a project folder mkdir taskmanager && cd taskmanager # Create a virtual environment python -m venv env # Activate the virtual environment (Windows) venv\Scripts\activate # Activate the virtual environment (Mac/Linux) source env/bin/activate
Once activated, your terminal will show (env), confirming the virtual environment is active.
Step 2: Install Django and Django REST Framework
Next, install Django and Django REST Framework using pip:
pip install django djangorestframework
Verify the installation:
python -m django --version
You’ll see the Django version number if installed correctly.
Django Version Check
Now, update your INSTALLED_APPS in taskmanager/settings.py to include DRF:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', ]
Step 3: Start a Django Project
Initialize a new Django project called taskmanager:
django-admin startproject taskmanager .
The dot (.) ensures the project is created in the current directory.
Your project structure should now look like this:
taskmanager/ │── taskmanager/ # Project settings │── manage.py # Django management script │── env/ # virtual env
Test the setup by running the server:
python manage.py runserver
Visit http://127.0.0.1:8000/ in your browser to see Django’s welcome page.
Step 4: Create a Django App for Tasks
Django projects are made up of smaller apps. So let’s create an app called tasks:
python manage.py startapp tasks
Now, the Updated project structure should look like this:
taskmanager/ │── tasks/ # New Django app │── taskmanager/ # Project settings │── manage.py # Django management script │── env/ # virtual env
Register the app in taskmanager/settings.py:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', # Django REST Framework 'tasks', # Register the tasks app ]
Step 5: Configure Django REST Framework
DRF provides many built-in features like authentication, serialization, and request handling. Add this configuration to taskmanager/settings.py:
REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication', ), 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.AllowAny', # Change this later for security ), }
This setup:
- Enables basic authentication for API requests
- Allows anyone to access the API (we'll restrict this later)
Step 6: Create a Simple Model for Tasks
Define a Task model in tasks/models.py:
from django.db import models class Task(models.Model): title = models.CharField(max_length=255) description = models.TextField(blank=True, null=True) completed = models.BooleanField(default=False) created_at = models.DateTimeField(auto_now_add=True) def __str__(self): return self.title
Apply the model to the database:
python manage.py makemigrations tasks python manage.py migrate
Conclusion
Congrats! You’ve set up a Django project with Django REST Framework and created a tasks app for your Task Manager API. You’re now ready to build API endpoints!
What’s Next?
What’s Next?
- In Part 2:, CRUD Operations, you’ll create API endpoints to manage tasks.
- Next, secure your API with Part 3: Authentication and Permissions.
- In Part 4: Personalize with User-Owned Tasks.
- Part 5: Optimizing API Performance.
- Part 6:Best Practices for Securing APIs.
- Part 7:API Documentation with OpenAPI and Swagger.
📌 Summary Checklist
- ✅ Set up a virtual environment
- ✅Installed Django and DRF
- ✅Created a Django project and app
- ✅ Configured DRF in settings.py
- ✅ Defined a Task model
- ✅ Ran migrations to apply database changes
💬 Was this guide helpful? Drop a comment below and let me know!😊
Top comments (2)
Where are the completion?
Thank you for this. I will compile all the links to the blogs and add them here.