gpt4 book ai didi

python - 类型错误 : cannot unpack non-iterable bool object

转载 作者:行者123 更新时间:2023-11-28 22:09:57 25 4
gpt4 key购买 nike

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

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

最佳答案

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

应该是username=username而不是username==username

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

关于python - 类型错误 : cannot unpack non-iterable bool object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57057211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com