0

I have one view which has POST and GET request. For POST request data comes from other URL. I don't use this view to POST the data. However, I have a GET request for the same view which retrieves data from the model and displays it. Now, when I open (GET request) this view it correctly shows the data. But in addition it shows text area for a POST request as well. I want to hide POST request on my view.

Code:

@api_view(['POST','GET',])
def TestView(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        customers_instance = Customers.objects.create(firstname=data[0]["value"],
                                            lastname=data[1]["value"],
                                            dob=data[2]["value"],
                                            emailaddress=data[3]["value"],
                                            address1=data[4]["value"],
                                            address2=data[5]["value"],
                                            city=data[6]["value"],
                                            state=data[7]["value"],
                                            postalcode=data[8]["value"])
        return HttpResponse('Data has been received by API')

    if request.method == 'GET':
        qs= Customers.objects.values('emailaddress','customer_id')
        serializer_class = CustomersKeySerializer
        return Response(serializer_class(many=True).to_representation(qs))

enter image description here

1
  • What you mean by " For POST request data comes from other URL" ? It's not relevant where the data comes from. If you disallow the POST requests to the particular view, it wont be accessible for any POST requests...from anywhere
    – JPG
    Apr 27, 2019 at 2:18

1 Answer 1

0

Jay,

I think you should use two different views since you are dealing with two different URL's: one for GET and one for POST request.

@api_view(['GET',])
def TestGetView(request):
    if request.method == 'GET':
        qs= Customers.objects.values('emailaddress','customer_id')
        serializer_class = CustomersKeySerializer
        return Response(serializer_class(many=True).to_representation(qs))


@api_view(['POST',])
def TestPostView(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        customers_instance = Customers.objects.create(firstname=data[0]["value"],
                                        lastname=data[1]["value"],
                                        dob=data[2]["value"],
                                        emailaddress=data[3]["value"],
                                        address1=data[4]["value"],
                                        address2=data[5]["value"],
                                        city=data[6]["value"],
                                        state=data[7]["value"],
                                        postalcode=data[8]["value"])
        return HttpResponse('Data has been received by API')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.