[Fixed]-Django returns 404 after using restframework into existing project

1👍

According to the comments above, this is happening cause of a wrong hardcoded URL by your side.

Instead of visiting http://127.0.0.1:8000/api/posts/1/like yous should be visiting http://127.0.0.1:8000/posts/api/1/like.

Notice that inside your root urls.py you have this:

url(r'^posts/', include("posts.urls", namespace='posts'))

and inside the posts.urls you have:

url(r'^api/(?P<id>[\w-]+)/like/$', views.PostLikeAPIToggle.as_view(), name='like-api-toggle')

So, the correct structure of the URL is this:

[--- from root urls.py ----] [--from posts urls--]
http://127.0.0.1:8000/posts/      api/1/like/

You can also determine the url like this:

> ./manage.py shell

from django.urls import reverse

print(reverse('posts:like-api-toggle', kwargs={'id': 1}))

Note: You can also remove the app_name = 'posts' from your app‘s urls.py. You are defining the namespace inside your root urls.py (include("posts.urls", namespace='posts'))

Hope that helps you!

👤nik_m

Leave a comment