[go: up one dir, main page]

0% found this document useful (0 votes)
22 views16 pages

TS-11 Django REST Framework 1

Uploaded by

Nazmul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views16 pages

TS-11 Django REST Framework 1

Uploaded by

Nazmul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

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@hotmail.com,
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 settings.py:
• INSTALLED_APPS = [
• ...
• 'rest_framework’,
• ]
3. Confi gure DRF settings (optional):
• REST_FRAMEWORK = {
• 'DEFAULT_PERMISSION_CLASSES': [
• 'rest_framework.permissions.AllowAny',
• ],
• 'DEFAULT_RENDERER_CLASSES': [
• 'rest_framework.renderers.JSONRenderer',
• 'rest_framework.renderers.BrowsableAPIRenderer',
• ],
• }
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 settings.py 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 manage.py 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(serializers.ModelSerializer):
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/urls.py:

2. Include the app's URLs in the main


urls.py
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
https://www.djang
l o-rest-framework.
Resource org/
REST API
s Concepts:
https://restfulapi.
net/
Q&A
Session

You might also like