Page not found Django restframework

I'm trying to write an API in Django to get the user comments from the URL.

My API is :

class ListSpecificUserCommentsApiView(APIView):
    authentication_classes = [authentication.TokenAuthentication]
    permission_classes = [permissions.AllowAny]
    
    def get(self, request: HttpRequest, user) -> Response:
        username: User = User.objects.get(id=user.id)
        comments: Comment = Comment.objects.filter(user=username)
        serialized_comments: CommentModelSerializer = CommentModelSerializer(instance=comments, many=True).data
        
        return Response(serialized_comments)

and my urls.py :

urlpatterns = [
    path(
        route='users',
        view=views.ListUsersApiView.as_view(),
        name='users',
    ),
    path(
        route='comments/<user>',
        view=views.ListSpecificUserCommentsApiView.as_view(),
        name='user-comments',
    ),
]

and still I'm facing this Page not found error.

I tried to pass the user as data in DRF Response like the code below:

class ListSpecificUserCommentsApiView(APIView):
    authentication_classes = [authentication.TokenAuthentication]
    permission_classes = [permissions.AllowAny]
    
    def get(self, request: HttpRequest, user) -> Response:
        username: User = User.objects.get(id=user.id)
        comments: Comment = Comment.objects.filter(user=username)
        serialized_comments: CommentModelSerializer = CommentModelSerializer(instance=comments, many=True).data
        
        return Response(serialized_comments, data=user)

but it didn't work.

I didn't find any problems with the code. One suspected part is that there is no last '/' in urls path.

urlpatterns = [
    path(
        route='users',
        view=views.ListUsersApiView.as_view(),
        name='users',
    ),
    path(
        route='comments/<user>',
        view=views.ListSpecificUserCommentsApiView.as_view(),
        name='user-comments',
    ),
]

If you send a request by adding '/' at the end of the above path, a page not found error occurs.

Add the last '/' to each path and try again!

urlpatterns = [
    path(
        route='users/',
        view=views.ListUsersApiView.as_view(),
        name='users',
    ),
    path(
        route='comments/<user>/',
        view=views.ListSpecificUserCommentsApiView.as_view(),
        name='user-comments',
    ),
]

Additionally

urlpatterns = [
    ...
    path(
        route='comments/<user>/',
        view=views.ListSpecificUserCommentsApiView.as_view(),
        name='user-comments',
    ),
]

As far as I know, if you do not specify a type for a dynamic path, the value automatically delivered to that path is str type. -> str(delivered value)

class ListSpecificUserCommentsApiView(APIView):
    ...
    def get(self, request: HttpRequest, user) -> Response:
        username: User = User.objects.get(id=user.id)
        ...
        
        return Response(serialized_comments, data=user)

When you try to import a user object through User.object.get, the user.id you delivered tries to refer to id in str.

AttributeError occurs because the id attribute does not exist in the str type object.

Since you are trying to get a user object through id, please change the dynamic path and function as below.

#urls.py
urlpatterns = [
    ...
    path(
        route='comments/<int:user_id>/',
        view=views.ListSpecificUserCommentsApiView.as_view(),
        name='user-comments',
    ),
]

#views.py
class ListSpecificUserCommentsApiView(APIView):
    ...
    def get(self, request: HttpRequest, user_id) -> Response:
        username: User = User.objects.get(id=user_id)
        ...
        
        return Response(serialized_comments, data=user)

If you're trying to get a user object through a value other than id, please let me know in the comments what value you're going to get!

I Figured out that in this case Using APIView I have to create something like the code below as my Api View:

class ListSpecificUserCommentsApiView(APIView):
    permission_classes = [permissions.AllowAny]
    
    def get(self, request: HttpRequest, username) -> Response:
        try:
            user: User = User.objects.get(username=username)
        
        except User.DoesNotExist:
            return Response(
                {'error': 'User not Found'},
                status=status.HTTP_404_NOT_FOUND,
            )
            
        comments: Comment = Comment.objects.filter(user=user)
        serialized_comments: CommentModelSerializer = CommentModelSerializer(comments, many=True)
        
        return Response(
            data=serialized_comments.data, 
            status=status.HTTP_302_FOUND,
        )

And for the urls.py :

path(
        route='comments/<str:username>/',
        view=views.ListSpecificUserCommentsApiView.as_view(),
        name='user-comments',
    ),
Back to Top