首页 > 解决方案 > /logout/ 'tuple' 对象的 AttributeError 没有属性 'backend'

问题描述

我在 windows 10 中使用 Python3 编写了一个 django2 网络应用程序。我尝试配置 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'),
)

应用程序/后端.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

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

   connected
Internal Server Error: /logout/
Traceback (most recent call last):
  File "C:\Users\software\python\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\software\python\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\software\python\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\software\python\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\software\python\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\software\python\lib\site-packages\django\views\decorators\debug.py", line 76, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "C:\Users\software\python\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\software\python\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\software\python\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\software\python\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Users\software\python\lib\site-packages\django\contrib\auth\views.py", line 61, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "C:\Users\software\python\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\software\python\lib\site-packages\django\views\generic\edit.py", line 141, in post
    if form.is_valid():
  File "C:\Users\software\python\lib\site-packages\django\forms\forms.py", line 185, in is_valid
    return self.is_bound and not self.errors
  File "C:\Users\software\python\lib\site-packages\django\forms\forms.py", line 180, in errors
    self.full_clean()
  File "C:\Users\software\python\lib\site-packages\django\forms\forms.py", line 382, in full_clean
    self._clean_form()
  File "C:\Users\software\python\lib\site-packages\django\forms\forms.py", line 409, in _clean_form
    cleaned_data = self.clean()
  File "C:\Users\software\python\lib\site-packages\django\contrib\auth\forms.py", line 205, in clean
    self.user_cache = authenticate(self.request, username=username, password=password)
  File "C:\Users\software\python\lib\site-packages\django\contrib\auth\__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

标签: python-3.xdjangoldap

解决方案


您编写以下行:

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)

推荐阅读