Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于Django类的视图中的身份验证用户_Python_Django_Django Rest Framework_Python Decorators - Fatal编程技术网

Python 基于Django类的视图中的身份验证用户

Python 基于Django类的视图中的身份验证用户,python,django,django-rest-framework,python-decorators,Python,Django,Django Rest Framework,Python Decorators,我正试图重构我的django项目。 因此,我想从以下方面进行重构: @api_view([GET, POST]) @permission_classes((IsAuthenticated, VehiclePermissions, )) def inactive_vehicle_view(request): if request.method == "GET": con = CBaseUtil.get_prod_instance()

我正试图重构我的django项目。 因此,我想从以下方面进行重构:

    @api_view([GET, POST])
    @permission_classes((IsAuthenticated, VehiclePermissions, ))
    def inactive_vehicle_view(request):

        if request.method == "GET":
            con = CBaseUtil.get_prod_instance()
            vehicle_bo = VehicleBO()
            dongle_dao = OBDDongleDAO(con)

            since_days = int(request.GET.get("since", 28))

            vehicles = vehicle_bo.find_vehicles_by_user_context(request.user.details)

            return Response(vehicles, status=status_code, headers=get_headers(request))
要创建基于类的视图,请执行以下操作:

class InactiveVehicleView(View):

    @authentication_classes((BasicAuthentication, WebsiteAuthentication))
    @permission_classes((IsAuthenticated, VehiclePermissions, ))
    def dispatch(self, *args, **kwargs):
        return super(InactiveVehicleView, self).dispatch(*args, **kwargs)

    def get(self, request):
        con = CBaseUtil.get_prod_instance()
        vehicle_bo = VehicleBO()
        dongle_dao = OBDDongleDAO(con)

        since_days = int(request.GET.get("since", 28))

        vehicles = vehicle_bo.find_vehicles_by_user_context(request.user.details)

        return Response(vehicles, status=status_code, headers=get_headers(request))
url(r'^vehicles/inactive/?$', InactiveVehicleView.as_view())
我面临的问题是,我无法通过request.user.details获得旧版本中的用户详细信息,因为WSGI请求不包含user属性。我想我和装修工做错了什么,但我想不出来

仅供参考,视图在URL中映射如下:

class InactiveVehicleView(View):

    @authentication_classes((BasicAuthentication, WebsiteAuthentication))
    @permission_classes((IsAuthenticated, VehiclePermissions, ))
    def dispatch(self, *args, **kwargs):
        return super(InactiveVehicleView, self).dispatch(*args, **kwargs)

    def get(self, request):
        con = CBaseUtil.get_prod_instance()
        vehicle_bo = VehicleBO()
        dongle_dao = OBDDongleDAO(con)

        since_days = int(request.GET.get("since", 28))

        vehicles = vehicle_bo.find_vehicles_by_user_context(request.user.details)

        return Response(vehicles, status=status_code, headers=get_headers(request))
url(r'^vehicles/inactive/?$', InactiveVehicleView.as_view())
有人知道我在身份验证和/或装饰器方面做错了什么吗?

根据,您的视图应该子类化
APIView
,并将
authentication\u classes
permission\u classes
设置为属性,而不是使用装饰器

from rest_framework.views import APIView

class InactiveVehicleView(APIView):

    authentication_classes = (BasicAuthentication, WebsiteAuthentication)
    permission_classes = (IsAuthenticated, VehiclePermissions)

    def get(self, request):
        ...

好的,我也试着把api_视图装饰器放在第二个,但这对任何事情都没有帮助。也许这就是你所需要的@danielcorreia,我已经在基于类的视图文档中尝试实现了。但是没有提示如何使用authentication\u classes和permission\u classes装饰器以及如何获取经过身份验证的用户的数据。谢谢,这解决了我的问题。除了基于类的教程之外,还应该检查docks。