How to fix 404 not found when I send POST method

34 views Asked by At

I am trying to send POST to my database through POSTMAN and I get 404 not Found status. Here is my views and urls.

#views
`class PlantViewSet(ModelViewSet):
    queryset = Plant.objects.all()
    serializer_class = PlantSerializer
    parser_classes = (MultiPartParser, FormParser)

    def create(self, request, *args, **kwargs):
        text = request.data["text"]
        price = request.data["price"]
        picture = request.data["picture"]

        Plant.objects.create(text=text, price=price, picture=picture)
        return Response("Plant created successfully", status=status.HTTP_200_OK)
`
#App.urls
`router = DefaultRouter()
router.register('plants/', views.PlantViewSet)
urlpatterns = [
    path('api/', include(router.urls))
]`

#project.urls
`
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('App.urls'))
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)`

I tried Google, ChatGPT, Youtube and none of them helped. Please help!

2

There are 2 answers

0
Mou Sam Dahal On

Going through what You have mentioned here, what you can do is check for all the urls that has been defined on your project, you can do so using the 'django_extensions' and following a simple command i.e. 'python manage.py show_urls'. Check this thread our if you need help, How can I list urlpatterns (endpoints) on Django?

0
wh0am1 On

Try using router.register("plants", PlantViewSet). Notice there's no / slash at the end. If you add an extra / at the end of url, it'll generate routes like this

/api/u//{username}/

Swagger Screenshot