TypeError: cannot unpack non-iterable bool object

20,294

Solution 1

You are making a mistake while filtering user.

It should be username=username and not username==username.

if User.objects.filter(username=username).exists():

Solution 2

In Python and many other programming languages, a single equal mark is used to assign a value to a variable, whereas two consecutive equal marks is used to check whether 2 expressions give the same value . ... (y==z) is True because we assign equal values to y and z.

if User.objects.filter(username==username).exists():

In this case we are not checking username is equal to username we are assigning and filtering value username to username in database so we use only single equal '='

if User.objects.filter(username=username).exists():

Share:
20,294
Maqbool Thoufeeq T
Author by

Maqbool Thoufeeq T

Updated on July 05, 2021

Comments

  • Maqbool Thoufeeq T
    Maqbool Thoufeeq T almost 3 years

    When I run the code it redirects and showing

    TypeError at /accounts/register cannot unpack non-iterable bool object
    

    The problem is when I put the below condition in my code

    User.objects.filter(username==username).exists():
    
    User.objects.filter(email=email).exists():
    
    

    Please help

    from django.shortcuts import render, redirect
    from django.contrib.auth.models import User, auth
    
    
    
    # Create your views here.
    
    def register(request):
    
        if request.method == 'POST':
            last_name = request.POST['Last_name']
            first_name = request.POST['first_name']
            username = request.POST['username']
            email = request.POST['email']
            password1 = request.POST['password1']
            password2 = request.POST['password2']
    
            if password1==password2:
                if User.objects.filter(username==username).exists():
                    print("username taken")
                elif User.objects.filter(email=email).exists():
                    print('email exists')
                else:
                    user = User.objects.create_user(username=username,password=password1,email=email,first_name=first_name,last_name=last_name)
                    user.save()
                    print('User Created')
            else:
                print('password doesnt match')
    
            return redirect('/')
    
    
        else:
            return render(request,'accounts/register.html')
    
    

    Error

    TypeError: cannot unpack non-iterable bool object
    
    [16/Jul/2019 17:51:22] "POST /accounts/register HTTP/1.1" 500 98878
    
    

    Environment:

    Request Method: POST
    Request URL: http://127.0.0.1:8000/accounts/register
    

    Traceback:

    File "C:\Users\MaqboolThoufeeqT\Envs\djangotest\lib\site-packages\django\core\handlers\exception.py" in inner
      34.             response = get_response(request)
    
    File "C:\Users\MaqboolThoufeeqT\Envs\djangotest\lib\site-packages\django\core\handlers\base.py" in _get_response
      115.                 response = self.process_exception_by_middleware(e, request)
    
    File "C:\Users\MaqboolThoufeeqT\Envs\djangotest\lib\site-packages\django\core\handlers\base.py" in _get_response
      113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)
    
    File "C:\Users\MaqboolThoufeeqT\Desktop\DjangoWork\travello\accounts\views.py" in register
      19.             if User.objects.filter(username==username).exists('True'):
    
    File "C:\Users\MaqboolThoufeeqT\Envs\djangotest\lib\site-packages\django\db\models\manager.py" in manager_method
      82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
    
    File "C:\Users\MaqboolThoufeeqT\Envs\djangotest\lib\site-packages\django\db\models\query.py" in filter
      892.         return self._filter_or_exclude(False, *args, **kwargs)
    
    File "C:\Users\MaqboolThoufeeqT\Envs\djangotest\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
      910.             clone.query.add_q(Q(*args, **kwargs))
    
    File "C:\Users\MaqboolThoufeeqT\Envs\djangotest\lib\site-packages\django\db\models\sql\query.py" in add_q
      1290.         clause, _ = self._add_q(q_object, self.used_aliases)
    
    File "C:\Users\MaqboolThoufeeqT\Envs\djangotest\lib\site-packages\django\db\models\sql\query.py" in _add_q
      1318.                     split_subq=split_subq, simple_col=simple_col,
    
    File "C:\Users\MaqboolThoufeeqT\Envs\djangotest\lib\site-packages\django\db\models\sql\query.py" in build_filter
      1187.         arg, value = filter_expr
    
    Exception Type: TypeError at /accounts/register
    Exception Value: cannot unpack non-iterable bool object
    
    • Daniel Roseman
      Daniel Roseman almost 5 years
      Please post the full traceback.
    • MisterMiyagi
      MisterMiyagi almost 5 years
      Where does the error occur? Your code does not unpack anything.
    • Maqbool Thoufeeq T
      Maqbool Thoufeeq T almost 5 years
      @DanielRoseman: ok Sir
    • Maqbool Thoufeeq T
      Maqbool Thoufeeq T almost 5 years
      @MisterMiyagi: I'' sent full traceback