期望从视图返回“ Response”,“ HttpResponse”或“ HttpStreamingResponse”,但是收到了“ <class'nonetype'=“”>”

时间:2019-01-04 18:23:12

标签: python django django-rest-framework django-views django-errors

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)

我也尝试过打印消息,但似乎未调用该方法

1 个答案:

答案 0 :(得分:4)

您应该在响应之前添加返回。

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)