djangoninja
djangoninja

Reputation: 163

Django - Receiving error "too many values to unpack (expected 2)"

In my website, I have created a custom user from the AbstractUser class. When I check if a user with the same username or email exists, I get a value error returned to me.

Here's my code: models.py:

class Account(AbstractUser):
    pass

    def __str__(self):
        return self.username

    def __unicode__(self):
        return self.username

views.py:

try:
   user = Account.objects.get(username=request.POST['username'])
   return render(request, 'users/signup.html', {'error': 'Username has already been taken'})
except Account.DoesNotExist:
           try:
               user = Account.objects.get(request.POST['email'])
               return render(request, 'users/signup.html', {'error': 'Email is not available'})
           except Account.DoesNotExist:
                        user = Account.objects.create_user(username=request.POST['username'], email=request.POST['email'], password=request.POST['password']) 
                        auth.login(request, user)
                        return redirect('home')

Error returned:

ValueError at /accounts/signup/
too many values to unpack (expected 2)

Traceback: user = Account.objects.get(username=request.POST['username'])

Ful traceback:

File "/Users/eesamunir/Documents/Projects/youtube-project/accounts/views.py" in signup
  28.                     user = Account.objects.get(username=request.POST['username'])

File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/query.py" in get
  406.             raise self.model.DoesNotExist(

During handling of the above exception (Account matching query does not exist.), another exception occurred:

File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/eesamunir/Documents/Projects/youtube-project/accounts/views.py" in signup
  32.                         user = Account.objects.get(request.POST['email'])

File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/query.py" in get
  399.         clone = self.filter(*args, **kwargs)

File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/query.py" in filter
  892.         return self._filter_or_exclude(False, *args, **kwargs)

File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/query.py" in _filter_or_exclude
  910.             clone.query.add_q(Q(*args, **kwargs))

File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/sql/query.py" in add_q
  1290.         clause, _ = self._add_q(q_object, self.used_aliases)

File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/sql/query.py" in _add_q
  1315.                 child_clause, needed_inner = self.build_filter(

File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/sql/query.py" in build_filter
  1187.         arg, value = filter_expr

Exception Type: ValueError at /accounts/signup/
Exception Value: too many values to unpack (expected 2)

Does anybody have a solution? Thank you.

Upvotes: 0

Views: 399

Answers (1)

Sawant Sharma
Sawant Sharma

Reputation: 758

Read this line carefully

File "/Users/eesamunir/Documents/Projects/youtube-project/accounts/views.py" in signup
32.user = Account.objects.get(request.POST['email'])

Error occurs due to this, It should be a key, value pair, Account.objects.get(email=request.POST['email'])

Upvotes: 3

Related Questions