如何定义APIView的网址?

时间:2019-01-21 21:05:13

标签: django django-rest-framework

我正在尝试使用Django Rest Framework创建APIView。当我将视图与URL关联时,出现此错误:

AssertionError: basename argument not specified, and could not automatically determine the name from the viewset, as it does not have a .queryset attribute.

这是我的APIView:

class ExampleView(APIView):
    authentication_classes = (SessionAuthentication, BasicAuthentication)
    permission_classes = (IsAuthenticated,)

    def get(self, request, format=None):
        content = {
            'user': unicode(request.user),  # `django.contrib.auth.User` instance.
            'auth': unicode(request.auth),  # None
        }
        return Response(content)

还有路由器:

router = routers.DefaultRouter()
router.register('api/example', views.ExampleView.as_view())

那么,怎么了?谢谢!

1 个答案:

答案 0 :(得分:1)

您只需要将路径添加到@ExceptionHandler(MethodArgumentTypeMismatchException::class) fun handleMethodArgumentTypeMismatch(ex: MethodArgumentTypeMismatchException, request: WebRequest) = 。路由器与视图集一起使用。

urlpatterns