Home > Software engineering >  Why is the route correct but still giving 404 error on Django
Why is the route correct but still giving 404 error on Django

Time:04-02

I got a 404 error on my website when accessing the correct route, did I do something wrong

urls.py handles the main route

from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'api/facebook/(. ?)', include('apifacebooks.urls')),
    path('', include('app.urls')),
    path('getcookie', include('app.urls')),
    path('change-lang', include('app.urls')),
    re_path(r'auth/(. ?)', include('app.urls')),
]

urls.py handles routes in the apifacebooks app

from django.urls import path
from . import views

urlpatterns = [
    path('api/facebook/like-post',views.like_post)
]

And when I go to http://localhost:8000/api/facebook/like-post I get a 404 error

Image error 404

My question has been solved, thanks

CodePudding user response:

In your apifacebooks app change the path because you had "api..." double in the path


from django.urls import path
from . import views

urlpatterns = [
    path('like-post/',views.like_post)
]

And in the root urls.py just

path('api/facebook/', ....)

CodePudding user response:

In your code, the url pattern would be "http://localhost:8000/api/facebook/api/facebook/like-post". As explained id Django docs:

Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.

Remove "api/facebook/" in "apifacebooks.urls", for example:

Main urls.py

from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'api/facebook/(. ?)/', include('apifacebooks.urls')),
    path('', include('app.urls')),
    path('getcookie', include('app.urls')),
    path('change-lang', include('app.urls')),
    re_path(r'auth/(. ?)', include('app.urls')),
]

apifacebooks.urls

from django.urls import path
from . import views

urlpatterns = [
    path('like-post',views.like_post)
]

Or you can try to place remove "r'api/facebook/(. ?)'", for example:

from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('apifacebooks.urls')),
    path('', include('app.urls')),
    path('getcookie', include('app.urls')),
    path('change-lang', include('app.urls')),
    re_path(r'auth/(. ?)', include('app.urls')),
]
  • Related