简体   繁体   English

django - DRF中的登录视图

[英]django - login view in DRF

I have a register view in my django code which works fine: 我的django代码中有一个注册视图,工作正常:

Working in Django rest framework, 在Django休息框架中工作,

class UserViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    @list_route(methods=['post'])
    def register(self, request):
        serializer = UserSerializer(data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Now I want to add a login view in order to let users login, I wrote something like this, not sure about this: 现在我想添加一个登录视图以便让用户登录,我写了这样的东西,不确定这个:

@list_route(methods=['post'])
    def login(self, request):
        username = request.POST['username']
        password = request.POST['password']

        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return Response(status=status.HTTP_200_OK)
            else:
                return Response(status=status.HTTP_404_NOT_FOUND)
        else:
            return Response(status=status.HTTP_401_UNAUTHORIZED)

couple of questions: 几个问题:

  • does it look okay? 看起来好吗?
  • and also, how should I POST to this view? 而且,我应该如何POST到这个视图? to the register view I POST with json and it works fine. 到寄存器视图我用json POST,它工作正常。 Will json with the username and password work here as well? 用户名和密码的json也可以在这里工作吗?

your *register is listening to post method with @list_route pattern and also you want to bind your *login to post again with @list_route ?!? 你的*注册是用@list_route模式监听post方法,你也想用@list_route绑定你的* login再次发帖?!?

it's imposible, if you ask me i prefer to implement my authentication class with APIView type 这是不可能的,如果你问我我更喜欢用APIView类型实现我的认证类

create your Login(views.APIView)like this: 像这样创建你的登录(views.APIView):


class LoginView(views.APIView):
    def post(self, request, format=None):
        data = request.data

        username = data.get('username', None)
        password = data.get('password', None)

        user = authenticate(username=username, password=password)

        if user is not None:
            if user.is_active:
                login(request, user)

                return Response(status=status.HTTP_200_OK)
            else:
                return Response(status=status.HTTP_404_NOT_FOUND)
        else:
            return Response(status=status.HTTP_404_NOT_FOUND)

You login method is not correct using DRF. 使用DRF登录方法不正确。 For login you haven't used the serialzers. 登录时你没有使用过串口。 Your can go through these api for login,logout etc for clear understanding 您可以通过这些api进行登录,注销等以获得清晰的理解

Djoser Djoser library provides a set of views to handle basic actions such as registration, login, logout, password reset and account activation. Djoser Djoser库提供了一组视图来处理注册,登录,注销,密码重置和帐户激活等基本操作。 The package works with a custom user model and it uses token based authentication. 该程序包使用自定义用户模型,并使用基于令牌的身份验证。 This is a ready to use REST implementation of Django authentication system. 这是一个随时可用的Django身份验证系统的REST实现。

django-rest-auth Django-rest-auth library provides a set of REST API endpoints for registration, authentication (including social media authentication), password reset, retrieve and update user details, etc. By having these API endpoints, your client apps such as AngularJS, iOS, Android, and others can communicate to your Django backend site independently via REST APIs for user management. django-rest-auth Django-rest-auth库提供了一组REST API端点,用于注册,身份验证(包括社交媒体身份验证),密码重置,检索和更新用户详细信息等。通过拥有这些API端点,您的客户端应用程序如此因为AngularJS,iOS,Android和其他人可以通过REST API独立地与您的Django后端站点进行通信以进行用户管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM