Ali Raza Javeed
Ali Raza Javeed

Reputation: 155

how could I POST the data in DJANGO rest_frame?

I want to insert Track record through a condition but it does not happen. Please help me how could I do fix it? this is views.py

class Track(APIView):
    http_method_names = ['post',]
    def post(self,request, *args, **kwargs):
        employee = Employee.objects.filter(username=kwargs.get('username'), password=kwargs.get('password'))
        if employee.exists():
            serializer_class = TrackSerializer
            try:
                if serializer_class.is_valid():
                    serializer_class.save()
                    return Response(serializer_class.data, status = status.HTTP_201_CREATED)
            except Exception as e:
                print(e)
                return Response(serializer_class.errors, status = status.HTTP_404_NOT_FOUND) 

urls.py

path('track/<username>/<password>/',views.TrackList.as_view(),name = 'track'),

Upvotes: 0

Views: 20

Answers (1)

Muhammad Hassan
Muhammad Hassan

Reputation: 14391

You have to pass data to your serializer

class Track(APIView):
    http_method_names = ['post',]
    def post(self,request, *args, **kwargs):
        employee = Employee.objects.filter(username=kwargs.get('username'), password=kwargs.get('password'))
        if employee.exists():
            serializer_class = TrackSerializer(data=request.data)
            try:
                if serializer_class.is_valid():
                    serializer_class.save()
                    return Response(serializer_class.data, status = status.HTTP_201_CREATED)
            except Exception as e:
                print(e)
                return Response(serializer_class.errors, status = status.HTTP_404_NOT_FOUND)

Upvotes: 2

Related Questions