MridulRao
MridulRao

Reputation: 70

'Page not found error' while creating rest-api using django rest framework

I declared the URL pattern correctly, but I get 'Page Not Found error' if I go to the 'localhost:8000/rest' URL. here is the code snippet -

from myspace import views
from rest_framework import routers
from rest_api import views as rest_api_views

router = routers.DefaultRouter()
router.register(r'post', rest_api_views.GetEntryViewSet)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('rest/',include('rest_framework.urls', namespace='rest_framework')),
    path('', include('myspace.urls')),
]

Upvotes: 0

Views: 1092

Answers (2)

vignesh
vignesh

Reputation: 39

Please Make sure you have installed and configured it correctly or not.

make sure have you included the URL. from django.conf.urls import include

For your reference

Upvotes: 0

VJ Magar
VJ Magar

Reputation: 1052

I think you missed the '/' in url. Try localhost:8000/rest/

Also, I think you need to update the `rest/' path in the urlpatterns

#current
path('rest/',include('rest_framework.urls', namespace='rest_framework')),

#updated
path('rest/', router.urls)

this is because currently, you are pointing to rest_frameworks.urls instead of the actual URLs.

Additionally, you can instead create the urls.py file in the rest_api app and move the routers related to the rest app into that.

# rest_api/urls.py
from rest_framework import routers
from rest_api import views as rest_api_views

router = routers.DefaultRouter()
router.register(r'post', rest_api_views.GetEntryViewSet)

urlpatterns = router.urls

Now use this file in the urls.py of the project.

from myspace import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('rest/',include('rest_api.urls', namespace='rest_api')),
    path('', include('myspace.urls')),
]

Upvotes: 2

Related Questions