Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
215 views

Django Restframework Tutorial

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)
215 views

Django Restframework Tutorial

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
You are on page 1/ 2

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 django.shortcuts import render


from django.http 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 = Task.objects.all()
seializer = TaskSerialzer(tasks, many=True)
return Response(seializer.data)

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

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

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

if serializer.is_valid():
serializer.save()
return Response(seializer.data)

@api_view([‘DELETE])
def taskDelete(request,pk):
tasks = Task.objects.get(id=pk)
task.delete()

return Response(‘Item successfully de)

4. on models file

from django.db import models

class Task(models.Model):
task=models.CharField(max_length=200)
completed = models.BooleanField(default=False)

def __str__(self):
return self.title

5. create a new file on the app named serializers.py

from rest_framework import serializers


from .models. import Task

class TaskSerializer(serializers.ModelSerializer):
class Meta:
model= Task
fields = ‘__all__’

6. on urls.py file
from django.urls import path
from .import views

urlpatterns = [
path(‘’, views.apiOverview, name=”api-overview”),
path(‘task-list/’, views.taskList, name=”task-list”),
path(‘task-detail/<str:pk>’, views.taskDetail, name=”task-detail”),
path(‘task-create/’, views.taskCreate, name=”task-create”),
path(‘task-update/<str:pk>’, views.taskUpdate, name=”task-update”),
path(‘task-delete/<str:pk>’, views.taskDelete, name=”task-delete”),

You might also like