Shreya
Shreya

Reputation: 644

Multiple Objects Returned Exception when entering wrong password in Django

I am using cookie cutter Django and on the url http://localhost:8000/accounts/login/ whenever I enter a wrong password I am getting the following traceback and a 500 Internal Server Error on the network console. I am using sqlite database. I am not able to understand why I am getting this error because when I create an empty project using the same database no such error is thrown.

 Environment:


Request Method: POST
Request URL: http://localhost:8000/accounts/login/

Django Version: 2.1.4
Python Version: 3.6.7
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'crispy_forms',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'rest_framework',
 'widget_tweaks',
 'core.users.apps.UsersAppConfig',
 'core.userManagement.apps.UsermanagementConfig',
 'debug_toolbar',
 'django_extensions']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware']



Traceback:

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
  45.         return bound_method(*args, **kwargs)

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
  76.             return view(request, *args, **kwargs)

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/allauth/account/views.py" in dispatch
  137.         return super(LoginView, self).dispatch(request, *args, **kwargs)

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/allauth/account/views.py" in dispatch
  80.                                             **kwargs)

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/allauth/account/views.py" in post
  102.         if form.is_valid():

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/django/forms/forms.py" in is_valid
  185.         return self.is_bound and not self.errors

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/django/forms/forms.py" in errors
  180.             self.full_clean()

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/django/forms/forms.py" in full_clean
  382.         self._clean_form()

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/django/forms/forms.py" in _clean_form
  409.             cleaned_data = self.clean()

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/allauth/account/forms.py" in clean
  179.             **credentials)

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/allauth/account/adapter.py" in authenticate
  489.         user = authenticate(request, **credentials)

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/django/contrib/auth/__init__.py" in authenticate
  73.             user = backend.authenticate(request, **credentials)

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/allauth/account/auth_backends.py" in authenticate
  26.             ret = self._authenticate_by_username(**credentials)

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/allauth/account/auth_backends.py" in _authenticate_by_username
  40.             user = filter_users_by_username(username).get()

File "/home/power/Documents/Projects/core/venv/lib/python3.6/site-packages/django/db/models/query.py" in get
  403.             (self.model._meta.object_name, num)

Exception Type: MultipleObjectsReturned at /accounts/login/
Exception Value: get() returned more than one User -- it returned 2!

Upvotes: 1

Views: 1588

Answers (2)

Mohsin Raza
Mohsin Raza

Reputation: 81

Check for Duplicate Social Applications in the Admin: Go to your Django Admin panel and check if there are multiple entries for the Google provider in the Social Applications section. This is where Django Allauth stores configuration for OAuth providers like Google, Facebook, etc.

Navigate to http://127.0.0.1:8000/admin/ Log in and find Social Applications under the Social Accounts section. Look for any duplicate entries for the Google application. There should be only one entry for the Google provider.

Delete Duplicate Social Applications: If you find multiple Google entries, delete all but one to ensure there is only a single Google OAuth2 configuration in the database.

Upvotes: 0

Peko Chan
Peko Chan

Reputation: 365

I'm currently working with the Django SocialAccount app and I came across a situation that might lead to conflicts. In the documentation, there's an example configuration for a Google SocialAccount provider:

SOCIALACCOUNT_PROVIDERS = {
    'google': {
        'APP': {
            'client_id': '123',
            'secret': '456',
            'key': ''
        }
    }
}

This configuration defines one Google SocialAccount provider. However, it's important to note that if you also manually added a provider in the Django admin interface, conflicts may arise. For instance, if you have another entry in the database, it could potentially conflict with the configuration in the code.

To prevent any issues, consider either commenting out the configuration in the code or removing the corresponding entry in the database through the admin interface. This ensures that there is a single source of truth for the Google SocialAccount provider configuration.

Hope it helps!

Upvotes: 0

Related Questions