ListAPIView返回页面找不到404错误而不是结果

时间:2019-07-23 16:53:15

标签: django django-rest-framework django-views django-urls

我有一个django rest框架应用程序,我想为特定的URL添加一个列表视图。当我转到URL并期望结果列表时,我得到了我的项目的URL列表和未找到页面的404消息。

这是我在视图中拥有的列表视图:

class UserHasPreferenceView(ListAPIView):
    serializer_class = PreferenceSerializer
    permission_classes = [IsAuthenticated]

    def get_queryset(self):
        namespace = self.kwargs.get('namespace', None)
        path = self.kwargs.get('path', None)
        filter_id = self.request.query_params.get('filter_id')
        if namespace and path and filter_id:
            queryset = Preference.objects.all().filter(
                person=filter_id, 
                namespace=namespace,
                path=path)
        elif namespace and path and filter_id is None:
            queryset = Preference.objects.all().filter(
                person=self.request.user.id,
                namespace=namespace,
                path=path
            )
        elif namespace and path is None and filter_id:
            queryset = Preference.objects.all().filter(
                person=filter_id,
                namespace=namespace
            )
        elif namespace and path is None and filter_id is None:
            queryset = Preference.objects.all().filter(
                person=self.request.user.id,
                namespace=namespace
            )
        elif namespace is None and path is None and filter_id:
            queryset = Preference.objects.all().filter(
                person=filter_id
            )
        elif namespace is None and path is None and filter_id is None:
            queryset = Preference.objects.all().filter(
                person=self.request.user.id
            )
        else:
            return None
        return queryset

这是网址

user_has_preference = UserHasPreferenceView.as_view()

path('person/has-preference/<str:namespace>/<str:path>/', user_has_preference,
         name='preferences-path'),

这是我得到的错误:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/api/v2/person/has-preference/

1 个答案:

答案 0 :(得分:0)

我看不到将person/has-preference/<str:namespace>/<str:path>/http://127.0.0.1:8000/api/v2/person/has-preference/进行匹配的方法,我想django也不能。这是路由定义而不是django rest的问题。