(https://i.sstatic.net/4OABY.png)
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.parsers import JSONParser
from rest_framework.decorators import api_view
from .models import note
from .serializers import noteSerializer
@api_view(['GET'])
def getroute(request):
routes = [
{ 'Endpoint':'/note/',
'method':'GET',
'body':None,
'description':'Returns an array of notes'},
{ 'Endpoint':'/note/id',
'method':'GET',
'body':None,
'description':'Returns an single note object'},
{ 'Endpoint':'/note/create/',
'method':'POST',
'body':{'body':""},
'description':'creates a single note object'},
{ 'Endpoint':'/note/id/update',
'method':'PUT',
'body':{'body':""},
'description':'updates a single note object'},
{ 'Endpoint':'/note/id/delete',
'method':'DELETE',
'body':None,
'description':'deletes a single note object'}
]
return Response(routes)
@api_view(['POST'])
def createnote(request):
print("request.data")
data = JSONParser().parse(request)
serializer = noteSerializer(data=data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
@api_view(['GET'])
def getNotes(request):
notes = note.objects.all().order_by('-updated')
serializer = noteSerializer(notes,many=True)
return Response(serializer.data)
@api_view(['GET'])
def getNote(request,pk):
notes = note.objects.get(id=pk)
serializer = noteSerializer(notes,many=False)
return Response(serializer.data)
@api_view(['PUT'])
def updatenote(request,pk):
data =request.data
notes =note.objects.get(id=pk)
serializer = noteSerializer(instance=notes,data=data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
@api_view(['Delete'])
def deletenote(request,pk):
notes = note.objects.get(id=pk)
notes.delete()
return Response("Note deleted")
from django.db import models
class note(models.Model):
body = models.TextField(null=True,blank=True)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
from rest_framework.serializers import ModelSerializer
from .models import note
class noteSerializer(ModelSerializer):
class Meta:
model = note
fields = '__all__'
At first I used the api/todo/create
url and it showed me "detail": "Method "GET" not allowed." and I added get method in the apiview of create and then I tried post a todo content through the Django rest api page and "detail": "JSON parse error - Expecting value: line 1 column 1 (char 0)" shows up but if I try the same with react, the post method works.
JSONParser()
. However, Django Rest Framework automatically parses the request data into a dictionary, so you don't need to parse it manually. You can access the data directly from request.data
. Removing the line data =
JSONParser().parse(request)
should resolve this issue.
@api_view(['POST'])
def createnote(request):
if request.method == 'POST':
serializer = noteSerializer(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)