DEV Community

Cover image for How to Build a Task Manager API with Django REST Framework (Step-by-Step Guide)
kihuni
kihuni

Posted on • Edited on

How to Build a Task Manager API with Django REST Framework (Step-by-Step Guide)

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

  1. Prerequisites
  2. Step 1: Create a Virtual Environment
  3. Step 2: Install Django and Django REST Framework
  4. Step 3: Start a Django Project
  5. Step 4: Create a Django App for Tasks
  6. Step 5: Configure Django REST Framework
  7. Step 6: Create a Simple Model for Tasks
  8. Conclusion
  9. What’s Next?
  10. 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
Enter fullscreen mode Exit fullscreen mode

Once activated, your terminal will show (env), confirming the virtual environment is active.

command line


Step 2: Install Django and Django REST Framework

Next, install Django and Django REST Framework using pip:

pip install django djangorestframework

Enter fullscreen mode Exit fullscreen mode

Verify the installation:

python -m django --version

Enter fullscreen mode Exit fullscreen mode

You’ll see the Django version number if installed correctly.
Django Version Check

django installation

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',
]
Enter fullscreen mode Exit fullscreen mode

Step 3: Start a Django Project

Initialize a new Django project called taskmanager:

django-admin startproject taskmanager .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Test the setup by running the server:

python manage.py runserver

Enter fullscreen mode Exit fullscreen mode

server running

Visit http://127.0.0.1:8000/ in your browser to see Django’s welcome page.

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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
]
Enter fullscreen mode Exit fullscreen mode

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
    ),
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Apply the model to the database:

python manage.py makemigrations tasks
python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

migrations


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?

In the next tutorial, we’ll create API endpoints using DRF to interact with the Task model. Stay tuned for Part 2!


📌 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 (0)