属性错误在 /logout/ 'tuple'对象没有属性'backend'



我在windows 10中使用Python3编写了一个django2 web应用程序。我尝试配置LDAP登录,但失败了。当我测试使用邮递员时,它可以成功地得到回复。也就是说,我向https://example.com/staff,带有一些身份验证代码和包含用户名和密码的有效负载,它用LDAP回复来回复我。

然而,当我尝试在Django中使用ldap3时,成功登录后,错误显示:

AttributeError at /logout/
'tuple' object has no attribute 'backend'

代码:settings.py:

AUTHENTICATION_BACKENDS = (
'app.backends.LDAPBackend',
('django.contrib.auth.backends.ModelBackend'),
)

app/backends.py:

from django.contrib.auth import get_user_model    
UserModel = get_user_model()
class LDAPBackend:
def authenticate(self, request, username=None, password=None, **kwargs):
try:      
headers = {'Authorization': xxxxx}        
body = {"username": username, "password": password}    
response = requests.post(url="https://example.com/staff", json=body, headers=headers)
result = response.json()
print(result)     
if result['code'] != "OK":
logger.info("Wrong Login Information")
return None
print("connected")
except Exception as e:
print(e)
user = UserModel.objects.update_or_create(username=username)
return user
def get_user(self, user_id):
try:
return UserModel._default_manager.get(pk=user_id)
except UserModel.DoesNotExist:
return None

错误显示:AttributeLogout/'tuple'对象处的AttributeError没有属性'backend',下面显示控制台中的结果:

connected
Internal Server Error: /logout/
Traceback (most recent call last):
File "C:Userssoftwarepythonlibsite-packagesdjangocorehandlersexception.py", line 34, in inner
response = get_response(request)
File "C:Userssoftwarepythonlibsite-packagesdjangocorehandlersbase.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:Userssoftwarepythonlibsite-packagesdjangocorehandlersbase.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:Userssoftwarepythonlibsite-packagesdjangoviewsgenericbase.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:Userssoftwarepythonlibsite-packagesdjangoutilsdecorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "C:Userssoftwarepythonlibsite-packagesdjangoviewsdecoratorsdebug.py", line 76, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "C:Userssoftwarepythonlibsite-packagesdjangoutilsdecorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "C:Userssoftwarepythonlibsite-packagesdjangoutilsdecorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:Userssoftwarepythonlibsite-packagesdjangoutilsdecorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "C:Userssoftwarepythonlibsite-packagesdjangoviewsdecoratorscache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:Userssoftwarepythonlibsite-packagesdjangocontribauthviews.py", line 61, in dispatch
return super().dispatch(request, *args, **kwargs)
File "C:Userssoftwarepythonlibsite-packagesdjangoviewsgenericbase.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "C:Userssoftwarepythonlibsite-packagesdjangoviewsgenericedit.py", line 141, in post
if form.is_valid():
File "C:Userssoftwarepythonlibsite-packagesdjangoformsforms.py", line 185, in is_valid
return self.is_bound and not self.errors
File "C:Userssoftwarepythonlibsite-packagesdjangoformsforms.py", line 180, in errors
self.full_clean()
File "C:Userssoftwarepythonlibsite-packagesdjangoformsforms.py", line 382, in full_clean
self._clean_form()
File "C:Userssoftwarepythonlibsite-packagesdjangoformsforms.py", line 409, in _clean_form
cleaned_data = self.clean()
File "C:Userssoftwarepythonlibsite-packagesdjangocontribauthforms.py", line 205, in clean
self.user_cache = authenticate(self.request, username=username, password=password)
File "C:Userssoftwarepythonlibsite-packagesdjangocontribauth__init__.py", line 80, in authenticate
user.backend = backend_path
AttributeError: 'tuple' object has no attribute 'backend'
[22/Mar/2021 09:49:10] "POST /logout/ HTTP/1.1" 500 149071

您编写以下行:

user = UserModel.objects.update_or_create(username=username)

update_or_create方法返回一个包含对象的元组和一个包含是否创建对象的布尔值。因此,您将这个元组存储在变量user中并返回它。但是authenticate应该只返回导致错误的意外效果的用户。此外,您应该使用get_or_create[Django-docs]而不是update_or_create

所以你应该把上面的行改成:

user, created = UserModel.objects.get_or_create(username=username)

最新更新