Home > database >  Why DRF giving me 404
Why DRF giving me 404

Time:04-03

I am keep getting 404 in DRF whenever I send request from postman even though the url is correct.

urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('backend.urls'))
]

backend/urls.py

from django.urls import path, include
from . import views
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register('/post', views.PostView, basename='post')

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

From postman I am sending post request on the endpoint http://127.0.0.1:8000/post but I am keep getting 404 not found error. What am I doing wrong?

CodePudding user response:

Try to change backend/urls.py:

import views
from django.urls import path, include
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register('post', views.PostView, basename='post')

urlpatterns = router.urls

CodePudding user response:

This is what works for me.

  • Create a app named backend using python manage.py startapp {app_name} where app_name is backend

  • Add the newly created app to INSTALLED_APPS in your project settings.py

  • In your project url.py file do this:

           from django.contrib import admin
           from django.urls import include, path
           urlpatterns = [
              path('admin/', admin.site.urls),
              path("", include("backend.urls")),
           ] 
    
    
  • In your app urls.py file. (where app is backend):

        from django.urls import include, path
        from rest_framework.routers import DefaultRouter
    
        from . import views
    
        router = DefaultRouter()
        router.register('post', views.PostView, basename='post')
    
        app_name = 'backend'
    
        urlpatterns = [
            path('', include(router.urls)),
        ]
    
  • Then in your app(backend) views.py define PostView

This should work.The endpoint should be at : http://localhost:8000/post/ .

if your using port 8000

  • Related