Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7k
Issue with POST Method Returning 405 on ModelViewSet#9627
-
Description: I'm experiencing an issue with a ModelViewSet in Django REST Framework. When I attempt to make a POST request to the registered URL, I receive the following error: 405 Method Not Allowed. However, the POST method is properly defined in my ViewSet. And I had another ViewSets that works fine. I don't understand why this one doesn't work, They are practically same. Below are the details of my setup:
Here is my code: classLaboratoryViewSet(viewsets.ModelViewSet):queryset=Laboratory.objects.all()serializer_class=LaboratorySerializerpermission_classes= [IsAuthenticated]defcreate(self,request,*args,**kwargs):print("POST method called")# Debuggingreturnsuper().create(request,*args,**kwargs) My Router router=DefaultRouter()router.register('lab',LaboratoryViewSet,basename='laboratory')urlpatterns= [path('api/pharmacies/',include(router.urls)), ] Troubleshooting Permissions: Temporarily removed all permissions to ensure they weren't blocking the request. |
BetaWas this translation helpful?Give feedback.
All reactions
Thank you for your help!
I’ve identified the issue. In my DefaultRouter, I had registered a ViewSet with an empty prefix('')
before other more specific routes like'lab/'
.
I had something like that:
router.register('', PharmacyViewSet, basename='pharmacy')router.register('lab', LaboratoryViewSet, basename='laboratory')
The problem was resolved by simply moving the registration of the ViewSet with the empty prefix('')
to the end of the router configuration.
Thanks again for pointing me in the right direction!
Replies: 1 comment 4 replies
-
I don't see a problem with what you provided. Which URL is your request calling? You said the method but not the host/path Do other HTTP verbs work? If yes, which ones? GET or OPTION might give you a clue... Have you tried browsing to the API root view? That might give you more information about what endpoints are available. |
BetaWas this translation helpful?Give feedback.
All reactions
-
I am making a POST request to : For the other HTTP verbs:
|
BetaWas this translation helpful?Give feedback.
All reactions
-
Weird, perhaps another view / route defined earlier is blocking access? If you install django-extensions, it has a |
BetaWas this translation helpful?Give feedback.
All reactions
-
Thank you for your help! I’ve identified the issue. In my DefaultRouter, I had registered a ViewSet with an empty prefix
The problem was resolved by simply moving the registration of the ViewSet with the empty prefix Thanks again for pointing me in the right direction! |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
I met the same problem, this was my initial code: router=DefaultRouter()router.register(r"api/v1/place/actors",PlaceActorsApi,basename="place_actors")router.register(r"api/v1/place/events",PlaceEventsApi,basename="place_events")router.register(r"api/v1/place/events/files",PlaceEventsFilesApi,basename="place_events_files")
router.register(r"api/v1/place/events/files",PlaceEventsFilesApi,basename="place_events_files")router.register(r"api/v1/place/events",PlaceEventsApi,basename="place_events") It could be nice to document it. |
BetaWas this translation helpful?Give feedback.