login(request, user) 函数在 django.contrib.auth 中不起作用。

huangapple go评论79阅读模式
英文:

login(request, user) function not working in django.contrib.auth

问题

我目前正在使用Django构建一个Web应用程序,需要用户登录,并且在views.py中有以下功能:

 def post(self, request, format=None):
        login = request.data
        user = authenticate(
                username=login['username'], password=login['password'])

        if user is not None:
            login(request, user)
            return Response({'message': '登录成功', 'status': 1})
        else:
            return Response({'message': '登录失败', 'status': 0})

但是登录函数一直触发错误:

File ".../views.py", line 58, in post
    login(request, user)
TypeError: 'dict'对象不可调用

我不知道为什么用户被识别为'dict'对象。请有人可以帮助我吗?

英文:

I am currently building a web app using Django that requires users to login, and I have this function in views.py

 def post(self, request, format=None):
        login = request.data
        user = authenticate(
                username=login['username'], password=login['password'])

        if user is not None:
            login(request, user)
            return Response({'message': 'Login successful', 'status': 1})
        else:
            return Response({'message': 'Login unsuccessful', 'status': 0})

But the login function keeps triggering an error

File ".../views.py", line 58, in post
    login(request, user)
TypeError: 'dict' object is not callable

I don't know why the user is identified as 'dict' object. Can please anyone help me?

答案1

得分: 0

更改变量名称将起作用。

英文:

login = request.data

changing the variable name will work.

huangapple
  • 本文由 发表于 2020年1月3日 15:36:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定