# Buggy code
def dispatch(self, request, *args, **kwargs):
    response = super(CompleteView, self).dispatch(request, *args, **kwargs)
    if self.request.user and self.request.user.is_authenticated():
        auth.login(self.request, self.request.user)
        logger.info(smart_text("User {} logged in".format(self.request.user.username)))
    return response


# Traceback (AttributeError)
2018/03/01 21:33:27 [warn] 31#0: *24 upstream server temporarily disabled while reading response header from upstream, client: 172.18.0.4, server: _, request: "GET /sso/complete/ HTTP/1.1", upstream: "uwsgi://127.0.0.1:8050", host: "awx.example.com", referrer: "https://logon.example.com/adfs/ls?SAMLRequest=<RESPONSE>&amp;RelayState=IDP"
new_obj = func(obj, *arg_vals)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/templatetags/rest_framework.py", line 244, in items
return value.items()
AttributeError: 'NoneType' object has no attribute 'items'
[pid: 42|app: 0|req: 2/8] 172.18.0.4 () {56 vars in 1674 bytes} [Thu Mar  1 21:33:26 2018] GET /sso/complete/ => generated 0 bytes in 271 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)
2018-03-01 21:33:27,330 ERROR    django.request Internal Server Error: /sso/complete/
Traceback (most recent call last):
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/utils/decorators.py", line 185, in inner
return func(*args, **kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/awx/sso/views.py", line 42, in dispatch
auth.login(self.request, self.request.user)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 149, in login
'You have multiple authentication backends configured and '
ValueError: You have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.
2018-03-01 21:33:27,330 ERROR    django.request Internal Server Error: /sso/complete/
Traceback (most recent call last):
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/utils/decorators.py", line 185, in inner
return func(*args, **kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/awx/sso/views.py", line 42, in dispatch
auth.login(self.request, self.request.user)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 149, in login
'You have multiple authentication backends configured and '
ValueError: You have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.
Traceback (most recent call last):
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 157, in __call__
response = self.get_response(request)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/base.py", line 124, in get_response
response = self._middleware_chain(request)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/exception.py", line 43, in inner
response = response_for_exception(request, exc)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/exception.py", line 93, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/exception.py", line 143, in handle_uncaught_exception
return callback(request, **param_dict)
File "/usr/lib/python2.7/site-packages/awx/main/views.py", line 88, in handle_500
return handle_error(request, 500, **kwargs)
File "/usr/lib/python2.7/site-packages/awx/main/views.py", line 56, in handle_error
return render(request, 'error.html', kwargs, status=status)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/shortcuts.py", line 30, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/loader.py", line 68, in render_to_string
return template.render(context, request)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 207, in render
return self._render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/loader_tags.py", line 177, in render
return compiled_parent._render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/loader_tags.py", line 177, in render
return compiled_parent._render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/loader_tags.py", line 72, in render
result = block.nodelist.render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/loader_tags.py", line 72, in render
result = block.nodelist.render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/defaulttags.py", line 40, in render
output = self.nodelist.render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/defaulttags.py", line 166, in render
values = self.sequence.resolve(context, True)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/template/base.py", line 736, in resolve
new_obj = func(obj, *arg_vals)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/templatetags/rest_framework.py", line 244, in items
return value.items()
AttributeError: 'NoneType' object has no attribute 'items'

# Fixed code
def dispatch(self, request, *args, **kwargs):
    response = super(CompleteView, self).dispatch(request, *args, **kwargs)
    if self.request.user and self.request.user.is_authenticated():
        logger.info(smart_text("User {} logged in".format(self.request.user.username)))
        response.set_cookie("userLoggedIn", "true")
        current_user = UserSerializer(self.request.user)
        current_user = JSONRenderer().render(current_user.data)
        current_user = urllib.quote("%s" % current_user, "")
        response.set_cookie("current_user", current_user)
    return response