Pradyum Gupta
Pradyum Gupta

Reputation: 317

ListCreateAPIView Django rest framework Method \"POST\" not allowed

See I don't know this is actually a very weird error that am receiving.

Inside Company App

views.py

class CompanyView(generics.ListCreateAPIView):
    queryset = CompanyProfile.objects.all()
    serializer_class = RegisterationCompanySerializer
    permission_classes = (permissions.IsAuthenticated,)

    def post(self, request, *args, **kwargs):
        import pdb; pdb.set_trace() #check weather post request is getting called?
        return self.create(request, *args, **kwargs)

    def get_serializer(self, *args, **kwargs):
        import pdb; pdb.set_trace() #check weather post serializer is getting called?
        user_id = self.request.user.id
        serializer_class = self.get_serializer_class()
        .....
        kwargs['data'] = draft_request_data
        return serializer_class(*args, **kwargs)

urls.py inside the company app

urlpatterns = [
    path('', include(router.urls)),

    ## Register Company Process
    # path('List/', ListCompanyView.as_view()),
    path('Reg/', CompanyView.as_view()),
]

serializer.py

class RegisterationCompanySerializer(serializers.ModelSerializer):

    class Meta:
        model = CompanyProfile
        fields = '__all__'
        extra_kwargs = {
                        'short_name': {'required': True},
                        ......
                        'user' : {'required': True},
                       }

models.py

class CompanyProfile(models.Model):
    id                  = models.UUIDField(default=uuid.uuid4, 
                                            editable=False, 
                                            primary_key=True,
                                            verbose_name='ID',)
   ......
   verified             = models.BooleanField(default=False)

    def __str__(self):
        return str(self.id)

Also, note the company is present in INSTALLED_APPS

But error am receiving this is on postman

{
    "detail": "Method \"POST\" not allowed."
}

on the terminal and tried PDB to check whether the function is getting called

Method Not Allowed: /company/Reg/
[Date and Time] "POST /company/Reg/ HTTP/1.1" 405 41

let me know if you are looking for more details.

Thanks in advance

Regards

Upvotes: 0

Views: 1082

Answers (2)

Sima Nongwe
Sima Nongwe

Reputation: 67

In your urls.py file make sure that your path ends with / e.g path('post/', Post.as_views()) and also on your http request include that '/' at the end. Thats how I solved the problem.

Upvotes: 0

Pradyum Gupta
Pradyum Gupta

Reputation: 317

With help of @ArakkalAbu, I was able to figure out the error this is how I updated the router

urlpatterns = [
    path('check/', include(router.urls)),
# 
    ## Register Company Process
    # path('List/', ListCompanyView.as_view()),
    path('Reg/', CompanyView.as_view()),
]

Now, this working fine, Thanks

Upvotes: 1

Related Questions