Lutaaya Huzaifah Idris
Lutaaya Huzaifah Idris

Reputation: 3990

Method \"POST\" not allowed in Django rest framework with extra actions & ModelViewset

I am trying to POST with extra actions in Django Rest, but am getting this error

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

This is the method for POST :

class TemplateView(ModelViewSet):
    @action(detail=False, methods=['POST'],
                url_path=r'process_version_template/')
        def process_version_template(self, request):
    
            print("request", request)
    
            return JsonResponse({}, safe=False, status=200)

In my urls.py :

api_router = DefaultRouter()
api_router.register(r'template', views.TemplateView, 'template')
urlpatterns = [
    path('predefined-comments', PredefinedCommentListView.as_view(),
         name='predefined_comments'),
]

urlpatterns += api_router.urls

Then my endpoint am using in Postman to send data is :

http://127.0.0.1:8000/api/v1/financing-settings/template/process_version_template/

then, the body is :

{
    "id":1784,
    "param":"block",
    "target":315
}

What am I doing wrong ?

Upvotes: 1

Views: 1948

Answers (1)

JPG
JPG

Reputation: 88589

It seems you don't need to put that trailing slash

Change

`url_path=r'process_version_template/'`

to

`url_path=r'process_version_template'`

Upvotes: 1

Related Questions