2

I have this url in my urls.py

 path('foo/bar/api', foo.APIBar.as_view(), name='foo-bar-api'),

and in my view.py I have this class that hands the api:

class APIBar(APIView):
def post(request, self, format=None):        
    date= request.POST['date']
    person= get_object_or_404(Person, id=request.POST['person'])  
    return Response(status=status.HTTP_201_CREATED)

And I'm trying to send this ajax:

$.ajax({
            url: "{% url 'foo-bar-api' %}",
            method: "POST",
            data: {
                date: date.val(),
                person: person.val()
            }
        });

But Django it's giving this error to me:

AttributeError: 'APIBar' object has no attribute 'POST'

I don't know why this is happening. I used the same structure in other models and works like a charm, but this one it's giving this error.

Please, can you tell me what am I doing wrong? I spent some hours trying to fix this error.

1 Answer 1

5

Your post method's arguments arrangements are wrong, correct ones should be:

def post(self, request, format=None):
    date= request.POST['date']
    person= get_object_or_404(Person, id=request.POST['person'])  
    return Response(status=status.HTTP_201_CREATED)

BTW, self here means object reference. So it should be the first argument of an Object Method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.