Home > Net >  django API views 404 not found
django API views 404 not found

Time:12-11

I am having trouble figuring this out. I can access http://127.0.0.1:8000/api/ But this returns 404 not found: http://127.0.0.1:8000/api/projects

I have been looking at the codes and couldn't find any errors. the terminal also showing this upon runserver: Not Found: /api/projects/ What could be the problem?

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.getRoutes),
    path('projects/', views.getProjects),
    path('project/<str:pk>/', views.getProjects),
]

views.py

from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response
from .serializers import ProjectSerializer
from projects.models import Project

@api_view(['GET'])
def getRoutes(request):

    routes = [
        {'GET': '/api/projects'},
        {'GET': '/api/projects/id'},
        {'POST': '/api/projects/id/vote'},

        {'POST': '/api/users/token'},
        {'POST': '/api/users/token/refresh'},
    ]
    return Response(routes)

@api_view(['GET'])
def getProjects(request):
    projects = Project.objects.all()
    serializer = ProjectSerializer(projects, many=True)
    return Response(serializer.data)

The 404 error page showing this:

Using the URLconf defined in djangoProject1.urls, Django tried these URL patterns, in this order:

    admin/
    projects/
    login/ [name='login']
    logout/ [name='logout']
    register/ [name='register']
    [name='profiles']
    profile/<str:pk> [name='user-profile']
    account/ [name='account']
    edit-account/ [name='edit-account']
    create-skill/ [name='create-skill']
    update-skill/<str:pk>/ [name='update-skill']
    delete-skill/<str:pk>/ [name='delete-skill']
    inbox/ [name='inbox']
    message/<str:pk>/ [name='message']
    create-message/<str:pk>/ [name='create-message']
    api
    api projects/
    api project/<str:pk>/
    reset_password/ [name='reset_password']
    reset_password_sent/ [name='password_reset_done']
    reset/<uidb64>/<token>/ [name='password_reset_confirm']
    reset_password_complete/ [name='password_reset_complete']
    ^images/(?P<path>.*)$
    ^static/(?P<path>.*)$

The current path, api/projects/, didn’t match any of these.

CodePudding user response:

Found the answer to my own problems. I was missing a / in my main project folder url.py

missing slash

CodePudding user response:

You missed the slash: http://127.0.0.1:8000/api/projects/

To prevent this problem next time, add APPEND_SLASH = True in your settings.py

APPEND_SLASH

  • Related