Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django: URL path is not working / 404 Page not found

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading