Mastering Django Framework: Building Scalable Web Applications
Course Title: Mastering Django Framework: Building Scalable Web Applications Section Title: Building RESTful APIs with Django REST Framework Topic: Develop a RESTful API for a task management application using Django REST Framework.(Lab topic)
Objective: By the end of this topic, you will be able to design and implement a RESTful API for a task management application using Django REST Framework (DRF). You will learn how to create API endpoints, handle authentication, and implement CRUD operations.
Prerequisites:
- Familiarity with Django and Django REST Framework
- Understanding of RESTful API design principles
- Basic knowledge of Python and Django
Step 1: Setting up the Project
To start, create a new Django project and install Django REST Framework using pip:
django-admin startproject task_manager
cd task_manager
pip install djangorestframework
Add 'rest_framework' to the INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
...
'rest_framework',
'task_manager.api', # New app for API
]
Create a new app for the API:
python manage.py startapp api
Step 2: Defining the API Endpoints
Create a new file serializers.py
in the api app to define the API endpoints:
from rest_framework import serializers
from .models import Task
class TaskSerializer(serializers.ModelSerializer):
class Meta:
model = Task
fields = ['id', 'title', 'description', 'completed']
Create a new file views.py
in the api app to define the API views:
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Task
from .serializers import TaskSerializer
class TaskList(APIView):
def get(self, request):
tasks = Task.objects.all()
serializer = TaskSerializer(tasks, many=True)
return Response(serializer.data)
def post(self, request):
serializer = TaskSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Step 3: Implementing Authentication
To implement authentication, create a new file authentication.py
in the api app:
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
class AuthenticationView(APIView):
authentication_classes = [SessionAuthentication]
permission_classes = [IsAuthenticated]
def get(self, request):
return Response({'message': 'Hello, authenticated user!'})
Step 4: Creating API Endpoints
Create a new file urls.py
in the api app to define the API endpoints:
from django.urls import path
from . import views
urlpatterns = [
path('tasks/', views.TaskList.as_view()),
path('tasks/<int:pk>/', views.TaskDetail.as_view()),
]
Step 5: Running the API
Run the API using the following command:
python manage.py runserver
Access the API using a tool like curl or Postman:
curl http://localhost:8000/api/tasks/
This should return a list of tasks.
Conclusion:
In this topic, you learned how to design and implement a RESTful API for a task management application using Django REST Framework. You created API endpoints, handled authentication, and implemented CRUD operations.
Additional Resources:
- Django REST Framework documentation: <https://www.django-rest-framework.org/>
- RESTful API design principles: <https://www.restapitutorial.com/>
Leave a comment or ask for help if you have any questions or need further clarification on any of the steps.
Images

Comments