Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'NoneType'>`

masaterofnothing picture masaterofnothing · Jan 4, 2019 · Viewed 10.9k times · Source

models.py

class Wishlist(models.Model):
    home = models.ForeignKey(Home, on_delete=models.CASCADE, blank=False, null=False)
    user = models.ForeignKey(User, on_delete=models.CASCADE, blank=False, null=False)

    def __str__(self):
        return "{} - {}".format(self.user.username, self.home.address)

    class Meta:
        ordering = ('user',)

urls.py

urlpatterns = [
    path(r'wishlist/<int:pk>/', views.WishlistGetUserItems.as_view(), name='wish-user-list'),
]

views.py

class WishlistGetUserItems(APIView):

    def get(self, request, pk):
        wishlist = get_list_or_404(Wishlist, user=pk)
        data = serializers.UserWishlistSerializer(wishlist).data
        Response(data, status=status.HTTP_200_OK)

I've tried also to print message but seems like the method is not called

Answer

Top Top picture Top Top · Jan 13, 2021

You should add return before Response.

def get(self, request, pk):
    wishlist = get_list_or_404(Wishlist, user=pk)
    data = serializers.UserWishlistSerializer(wishlist).data
    return Response(data, status=status.HTTP_200_OK)