2021年3月21日星期日

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

I used Python3 in windows 10 to write a django2 web app. I tried to configure the LDAP login, but failed. When I test using postman to test, it could get the reply successfully. That is, I send a request to https://example.com/staff, with some authentication code and payload containing username and password, and it reply me with the LDAP reply.

However, when I tried to using ldap3 in Django, after successfully login, error shows:

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

code: settings.py:

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

app/backends.py:

class LDAPBackend:    def authenticate(self, request, username=None, password=None, **kwargs):        try:          print("begin connect!")          headers = {'Authorization': xxxxx}          print(headers)          body = {"username": username, "password": password}          print(body)          response = requests.post(url="https://example.com/staff", json=body, headers=headers)          result = response.json()            print(result)          logger.info(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  

error shows: AttributeError at /logout/ 'tuple' object has no attribute 'backend', below shows result in console:

   connected  Internal Server Error: /logout/  Traceback (most recent call last):    File "C:\Users\williamyangy\Bill\software\python\lib\site-packages\django\core\handlers\exception.py", line 34, in inner      response = get_response(request)    File "C:\Users\williamyangy\Bill\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\williamyangy\Bill\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\williamyangy\Bill\software\python\lib\site-packages\django\views\generic\base.py", line 71, in view      return self.dispatch(request, *args, **kwargs)    File "C:\Users\williamyangy\Bill\software\python\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper      return bound_method(*args, **kwargs)    File "C:\Users\williamyangy\Bill\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\williamyangy\Bill\software\python\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper      return bound_method(*args, **kwargs)    File "C:\Users\williamyangy\Bill\software\python\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view      response = view_func(request, *args, **kwargs)    File "C:\Users\williamyangy\Bill\software\python\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper      return bound_method(*args, **kwargs)    File "C:\Users\williamyangy\Bill\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\williamyangy\Bill\software\python\lib\site-packages\django\contrib\auth\views.py", line 61, in dispatch      return super().dispatch(request, *args, **kwargs)    File "C:\Users\williamyangy\Bill\software\python\lib\site-packages\django\views\generic\base.py", line 97, in dispatch      return handler(request, *args, **kwargs)    File "C:\Users\williamyangy\Bill\software\python\lib\site-packages\django\views\generic\edit.py", line 141, in post      if form.is_valid():    File "C:\Users\williamyangy\Bill\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\williamyangy\Bill\software\python\lib\site-packages\django\forms\forms.py", line 180, in errors      self.full_clean()    File "C:\Users\williamyangy\Bill\software\python\lib\site-packages\django\forms\forms.py", line 382, in full_clean      self._clean_form()    File "C:\Users\williamyangy\Bill\software\python\lib\site-packages\django\forms\forms.py", line 409, in _clean_form      cleaned_data = self.clean()    File "C:\Users\williamyangy\Bill\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\williamyangy\Bill\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  
https://stackoverflow.com/questions/66739227/attributeerror-at-logout-tuple-object-has-no-attribute-backend March 22, 2021 at 09:18AM

没有评论:

发表评论