英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论