1👍
Your post
is a method, so the first parameter is self
:
class SubscriptionView(APIView):
queryset = Subscription.objects.all()
def post(self, request):
email = request.data.get('email')
Subscription.objects.create(email=email)
return Response(status=status.HTTP_201_CREATED)
It is however quite seldom that one implements a post
method itself. Usually you work for example with a CreateAPIView
[drf-doc], and one can let the serializer, etc. handle all the work.
0👍
post is an Instance method of Class SubscriptionView
, while invoking an Instance method Python automatically passes an object reference
of that class. So you need to receive that object reference in your instance method.
You didn’t receive that object reference in your post method that’s why Python Interpreter showing you:
TypeError: post() takes 1 positional argument but 2 were given
but 2 were given
Because Python calling post like, post(SubscriptionView(), request)
but you receive only request
You can receive object reference
using any name but using self
is standard.
class SubscriptionView(APIView):
queryset = Subscription.objects.all()
def post(self, request):
email = request.data.get('email')
Subscription.objects.create(email=email)
return Response(status=status.HTTP_201_CREATED)
- [Answered ]-Are there any php frameworks contain small webserver and rich console tools like RoR/Django?
- [Answered ]-Django forms – display HTML <pre> code snippets within rendered {{ form.text }}
- [Answered ]-How do i access a dictionary value in a django template?
- [Answered ]-Django ListView __init__ accepting keyword arguments
- [Answered ]-How to change the key of the form passed to a template using a class based view