Search code examples
pythondjangodjango-viewsdjango-templates

Why do I get the error "cannot unpack non-iterable bool object" in Django?


I get the error "cannot unpack non-iterable bool object" as I try to get user input from a form in django which in this case is an email.

def send_email(request):
      recepient_list=[]
      if request.method == "POST":
            
            email = request.POST['pwd_email']
            print(email)
            if User.objects.filter(email == email).exists:
                  recepient_list.append(email)
                  send_mail("PASSWORD RESET", f"Your password reset key is {random_no} \n Do not share this key with anyone", "eliaakjtrnq@gmail.com",recepient_list,fail_silently=False,)
                  recepient_list.clear()

Solution

  • There are several problems here, the main one is .filter(email == email). This will check if the string is equal to itself, and therefore always True, you thus use .filter(True), and Django can of course not do much with the bool.

    You should filter with:

    def send_email(request):
        recepient_list = []
        if request.method == 'POST':
            email = request.POST['pwd_email']
            print(email)
            #                      ***call*** .exists() 🖟
            if User.objects.filter(email=email).exists():
                recepient_list.append(email)
                send_mail(
                    'PASSWORD RESET',
                    f"Your password resetkey is {random_no} \n Do not share this key with anyone",
                    'eliaakjtrnq@gmail.com',
                    recepient_list,
                    fail_silently=False,
                )
                recepient_list.clear()