TypeError:无法解包不可迭代的布尔对象

时间:2019-07-16 12:23:15

标签: python django python-3.x django-views python-requests

运行代码时,它会重定向并显示

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

问题是当我将以下条件放入代码中

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

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

请帮助

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')

错误

TypeError: cannot unpack non-iterable bool object

[16/Jul/2019 17:51:22] "POST /accounts/register HTTP/1.1" 500 98878

环境:

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

跟踪:

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

2 个答案:

答案 0 :(得分:1)

您在过滤用户时犯了一个错误。

应该为username=username,而不是username==username

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

答案 1 :(得分:0)

在 Python 和许多其他编程语言中,单个等号用于为变量赋值,而两个连续的等号用于检查 2 个表达式是否给出相同的值。 ... (y==z) 为真,因为我们为 y 和 z 分配了相等的值。

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

在这种情况下,我们不检查用户名是否等于我们在数据库中为用户名分配和过滤值用户名的用户名,因此我们只使用单个等于 '='

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