0

I want to Request a dictionary like

{
    "username": "a",
    "password": "b",
    "year": "2019-20",
    "start": 1,
    "end": 2,
    "reference_id": "two"
  }

from a user so that a user can hit the API and get the desired result.

My view looks like


    def post(self,request, *args, **kwargs):
        # self.http_method_names.append("post")
        user = request.POST.get("user")
        pasd = request.POST.get("pasd")
        year = request.POST.get("year")
        start = request.POST.get("start")
        end = request.POST.get("end")
        reference_id = request.POST.get("reference_id")
        #Passing the parameters to another function to get the result. 
        status = main_function(user=user,pasd=pasd,year=year,start=start,end=end,reference_id=reference_id)
        return Response(status)


Now the problem when I'm posting something in Django like enter image description here

I'm getting None in every field. Ideally, I should get the values passed in the Dictionary. Can someone please help me here.

0

2 Answers 2

0

In django rest framework, you should use request.data instead of request.POST, as it requires parser to receive JSON data. More info can be found in drf docs.

Change your code to:

def post(self,request, *args, **kwargs):
        # self.http_method_names.append("post")
        user = request.data.get("username") # <-- this used incorrect key
        pasd = request.data.get("password") # <-- this used incorrect key
        year = request.data.get("year")
        start = request.data.get("start")
        end = request.data.get("end")
        reference_id = request.data.get("reference_id")
        #Passing the parameters to another function to get the result. 
        status = main_function(user=user,pasd=pasd,year=year,start=start,end=end,reference_id=reference_id)
        return Response(status)
1
  • Thanks for you help. I tried it with serialzers and it worked.I will try you solution also. Dec 31, 2019 at 7:10
0

I Got The Answer. SO if You Want to take input from a user then You should write this in your views.py file.

But First you have to tell Django Which Fields input you want. So you have to create a serializer.py file in the Django APP.

from rest_framework import serializers


class userdetailSerializer(serializers.Serializer):
    username = serializers.CharField(max_length=20)
    password = serializers.CharField(max_length=20)
    year = serializers.CharField(max_length=8)
    start = serializers.IntegerField()
    end = serializers.IntegerField()
    reference_id = serializers.CharField(max_length=50)

I need all the Above values from the User that's why i have added all. If you want Only certain values you can delete it from your serializer file.

After this goto your Views.py file and get the response entered by user using below code. Serializer will autovalidate the input and if the input is not correct it will give a dictionary of error(In else Statement)

  def post(self,request, *args, **kwargs):
        # self.http_method_names.append("post")
        serializer = self.serializer_class(data=request.data)

        if serializer.is_valid():
            username = serializer.validated_data.get('username')
            password = serializer.validated_data.get('password')
            year = serializer.validated_data.get('year')
            start = serializer.validated_data.get('start')
            end = serializer.validated_data.get('end')
            reference_id = serializer.validated_data.get('reference_id')
            response_status = main_function(user=username, pasd=password,year=year,start=start,end=end,reference_id=reference_id)
            return Response("The File Upload Response Will Come HEre",response_status)
            # return Response({'name':username, 'pass':password,'year':year,'start':start,'end':end,'reference_id':reference_id})
        else:
            return Response(serializer.errors)

The POST API will LOOk Like this.

enter image description here

If you guys are facing an error here then Plese comment. Upvote if it helps.

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.