简体   繁体   English

Django Rest Framework-对象没有属性发布

[英]Django Rest Framework - object has no attribute post

I have this url in my urls.py 我的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: 在我的view.py中,我有这个类来处理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:

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

But Django it's giving this error to me: 但是Django它给了我这个错误:

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. 我花了几个小时尝试修复此错误。

Your post method's arguments arrangements are wrong, correct ones should be: 您的post方法的参数安排有误,正确的参数应为:

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. 顺便说一句,“ self在这里表示对象引用。 So it should be the first argument of an Object Method. 因此,它应该是对象方法的第一个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM