Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Issue with POST Method Returning 405 on ModelViewSet#9627

Discussion options

Description:
Hello,

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:
Context

Django REST Framework Version: 3.15.2Django Version: 5.1.3

Here is my code:
My ViewSet

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.
Method Check: Added a print statement inside the create method, but it never gets called.

You must be logged in to vote

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

Comment options

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.

You must be logged in to vote
4 replies
@NeyDiallo
Comment options

I am making a POST request to :http://127.0.0.1:8000/api/pharmacies/lab/.

For the other HTTP verbs:

  • GET on /lab/ returns a 404 Not found.

  • GET on /lab/1/ works as expected and retrieves the lab detail.

@browniebroke
Comment options

Weird, perhaps another view / route defined earlier is blocking access? If you install django-extensions, it has ashow_urls management command which might help you find this. Follow their docs for installing and runpython manage.py show_urls

@NeyDiallo
Comment options

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!

Answer selected bybrowniebroke
@hypsug0
Comment options

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")

POST requests onapi/v1/place/events/files returned a405 Method not allowed error, inverting the 2 last lines fixed the problem.

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.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
3 participants
@NeyDiallo@browniebroke@hypsug0

[8]ページ先頭

©2009-2025 Movatter.jp