Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: cannot unpack non-iterable bool object

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
like image 941
Maqbool Thoufeeq T Avatar asked Jul 16 '19 12:07

Maqbool Thoufeeq T


2 Answers

You are making a mistake while filtering user.

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

if User.objects.filter(username=username).exists():
like image 184
Astik Anand Avatar answered Oct 21 '22 05:10

Astik Anand


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():

like image 40
Sanjay RD Avatar answered Oct 21 '22 06:10

Sanjay RD