Issue
I'm trying to build an API with Django for the first time. To see if everything works, I tried to implement a get method at a "try/" endpoint and return a simple json containing "name":"myname".
The django app is running succesfully and the admin page works as intended. The endpoint "admin/try" is not reachable though and returns a 404.
I have all the files related to the api in a seperate folder, not in the folder of the actual app. This should, according to some tutorials, not be a problem.
This is the GET method I am trying to call at the "try/" endpoint. Its located in views.py in the api folder.
from rest_framework.response import Response
from rest_framework.decorators import api_view
@api_view(['GET'])
def tryMethod(request):
something = {'name':'myname'}
return Response(something)
This is the urls.py file in the api folder:
from django.urls import path
from . import views
urlpatterns = [
path('try/', views.tryMethod)]
This is the urls.py file in the folder of the actual app
from django.urls import path, include
from django.contrib import admin
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('health/', views.health, name='health'),
path('schema/', SpectacularAPIView.as_view(), name='schema'),
path('docs/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
path('404', views.handler404, name='404'),
path('500', views.handler500, name='500'),
path('', include('api.urls')),
]
This is the error screen: Error 404, Page not found
Solution
Based on your screen, I conclude that you are requested http://localhost:3000/admin/try
To get response from this:
@api_view(['GET'])
def tryMethod(request):
something = {'name':'myname'}
return Response(something)
You need request http://localhost:3000/try
Answered By - Moonar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.