0% found this document useful (0 votes)
97 views2 pages

Django REST Framework API Setup Guide

The document provides instructions for setting up a Django REST framework API. It includes installing Django REST framework, adding it to settings, creating views to list, detail, create, update and delete tasks, defining a Task model, serializing the model with a TaskSerializer, and setting up URLs to connect to the views. The API allows GET, POST, PUT, and DELETE requests to manage task objects in the database.

Uploaded by

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

Django REST Framework API Setup Guide

The document provides instructions for setting up a Django REST framework API. It includes installing Django REST framework, adding it to settings, creating views to list, detail, create, update and delete tasks, defining a Task model, serializing the model with a TaskSerializer, and setting up URLs to connect to the views. The API allows GET, POST, PUT, and DELETE requests to manage task objects in the database.

Uploaded by

ritesh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
  • Setup Instructions
  • API View Implementation
  • Model Definition
  • URL Configuration

1.

pip install djangorestframework


2. add rest_framework on settings file

# all the codes below are to be added on API folder (or any app created within)

3. on views file

from [Link] import render


from [Link] import JsonResponse

from rest_framework.decorators import api_view


from rest_framework.response import Response

from .models import Task


from .serializers import TaskSerializer

# what this decorator allows is get the GET response from api
@api_view([‘GET’])
def apiOverview(request):
api_url = {
‘List’:/task-list/’,
‘Detail View’:’task-detail/<str:pk>/’,
‘create’:’task-create/’,
‘update’:/task-update/<str:pk>/,
‘Delete’:’/task-delete/<str:pk/’
}

return Response(api_url)

@api_view([‘GET’])
def taskList(request):
tasks = [Link]()
seializer = TaskSerialzer(tasks, many=True)
return Response([Link])

@api_view([‘GET’])
def taskDetail(request,pk):
tasks = [Link](id=pk)
seializer = TaskSerialzer(tasks, many=False)
return Response([Link])

@api_view([‘POST])
def taskCreate(request):
seializer = TaskSerialzer(data=[Link])
if serializer.is_valid():
[Link]()
return Response([Link])

@api_view([‘POST])
def taskUpdate(request,pk):
tasks = [Link](id=pk)
seializer = TaskSerialzer(instance=task, data=[Link])

if serializer.is_valid():
[Link]()
return Response([Link])

@api_view([‘DELETE])
def taskDelete(request,pk):
tasks = [Link](id=pk)
[Link]()

return Response(‘Item successfully de)

4. on models file

from [Link] import models

class Task([Link]):
task=[Link](max_length=200)
completed = [Link](default=False)

def __str__(self):
return [Link]

5. create a new file on the app named [Link]

from rest_framework import serializers


from .models. import Task

class TaskSerializer([Link]):
class Meta:
model= Task
fields = ‘__all__’

6. on [Link] file
from [Link] import path
from .import views

urlpatterns = [
path(‘’, [Link], name=”api-overview”),
path(‘task-list/’, [Link], name=”task-list”),
path(‘task-detail/<str:pk>’, [Link], name=”task-detail”),
path(‘task-create/’, [Link], name=”task-create”),
path(‘task-update/<str:pk>’, [Link], name=”task-update”),
path(‘task-delete/<str:pk>’, [Link], name=”task-delete”),

1.  pip install djangorestframework
2. add rest_framework on settings file
# all the codes below are to be added on API folde
return Response(seializer.data)
@api_view([‘DELETE]) 
def taskDelete(request,pk): 
tasks = Task.objects.get(id=pk)
task.delet

You might also like