Django REST
Framework Part 1
Introduction to RESTful APIs, Setting
up Django REST Framework,
Creating API Endpoints
Program: EDGE-CSE CUET DIGITAL Instructor:
SKILLS TRAINING Tanveer Rahman,
Sr. Software Engineer,
Course: Python (Django) [Intermediate]
Friends Corp.
Lecture: Training Session-11 Chuo-ku, Tokyo, JP
Date: 18th October, 2024 shakil10vr@[Link],
Duration: (2+2) hours 01626735005
Agenda
Introduction to
Setting up Django
RESTful APIs
REST Framework
Creating API
Practical Tasks
Endpoints
REST
(Representational
•State Transfer):
Architectural style for
Introducti designing networked
on to applications.
• Uses standard HTTP
RESTful Key Concepts:
methods like GET, POST,
APIs PUT, DELETE.
• Resources: Data entities
(e.g., users, products).
• Endpoints: URL paths to
access resources.
•
Separation of client and
server: Allows independent
development.
Scalability: Each resource
Why Use can be managed
independently.
REST Flexibility: Supports
APIs? different formats like JSON,
XML.
Stateless Communication:
Each request is
independent.
Django REST Framework is a
powerful toolkit for building Web
Introducti APIs in Django.
on to Features:
Django • Authentication and permissions.
• Serialization for converting complex data
REST types.
• Browsable API for easier testing and
Framewor debugging.
Components:
k (DRF) • Serializers: Convert data between JSON
and Django models.
• Views: Handle requests and responses.
• Routers: Automatically generate URL
routes for views.
Setting Up Django REST
Framework
1. Install DRF using pip:
• pip install djangorestframework
2. Add 'rest_framework' to INSTALLED_APPS in [Link]:
• INSTALLED_APPS = [
• ...
• 'rest_framework’,
• ]
3. Confi gure DRF settings (optional):
• REST_FRAMEWORK = {
• 'DEFAULT_PERMISSION_CLASSES': [
• 'rest_framework.[Link]',
• ],
• 'DEFAULT_RENDERER_CLASSES': [
• 'rest_framework.[Link]',
• 'rest_framework.[Link]',
• ],
• }
Task 1 - Setting Up DRF (30
mins)
1. Install Django REST Framework in your
Django project.
2. Add 'rest_framework' to the
INSTALLED_APPS in the [Link] file.
3. Run the development server and ensure
there are no errors.
Creating Your First API
Endpoint
1. Create a Django app (e.g., books) for API development:
• python [Link] startapp books
2. Define a simple model (e.g., Book with fields for title
and content).
3. Create a serializer for the model:
from rest_framework import serializers
from .models import Book
class BookSerializer([Link]):
class Meta:
model = Book
fields = ‘__all__'
Writing Views for the API
1. Use DRF's built-in views (e.g., APIView
or GenericAPIView).
2. Create a basic view for listing and
creating posts:
Configuring URL Routing for
API Endpoints
1. Add URLs for the API in books/[Link]:
2. Include the app's URLs in the main
[Link]
TASK 2- Creating API
Endpoints (45 mins)
• Create a new Django app and define a
simple model (e.g., Author, Publisher).
• Create a serializer and API view to
handle GET and POST requests for the
model.
• Set up URL routing and test the
endpoints using Django's browsable API.
Use Django's
Browsable API:
• Navigate to
/api/books/rest/ to test
GET and POST requests.
Testing Testing Tools:
Your API • Postman or cURL for
making API requests.
Validation:
• Check response status
codes and JSON
structure.
Task 3 - Testing and
Extending the API (45 mins)
• Use Postman to make GET and POST
requests to your API.
• Extend your API by adding support for
retrieving, updating, and deleting
individual posts.
• Test all endpoints to ensure they work
as expected.
Recap
RESTFUL APIS: DJANGO REST CREATING API
ARCHITECTURAL FRAMEWORK: ENDPOINTS:
STYLE FOR TOOLKIT FOR DEFINE MODELS,
BUILDING WEB BUILDING WEB APIS SERIALIZERS,
SERVICES. WITH DJANGO. VIEWS, AND
DRF
Documentation:
Additiona
[Link]
l o-rest-framework.
Resource org/
REST API
s Concepts:
[Link]
net/
Q&A
Session