在我的网站中,我已经从 AbstractUser 类创建了一个自定义用户。 当我检查是否存在具有相同用户名或电子邮件的用户时,会收到返回值错误的信息。
这是我的代码: models.py :
class Account(AbstractUser):
pass
def __str__(self):
return self.username
def __unicode__(self):
return self.username
views.py :
try:
user = Account.objects.get(username=request.POST['username'])
return render(request, 'users/signup.html', {'error': 'Username has already been taken'})
except Account.DoesNotExist:
try:
user = Account.objects.get(request.POST['email'])
return render(request, 'users/signup.html', {'error': 'Email is not available'})
except Account.DoesNotExist:
user = Account.objects.create_user(username=request.POST['username'], email=request.POST['email'], password=request.POST['password'])
auth.login(request, user)
return redirect('home')
返回错误:
ValueError at /accounts/signup/
too many values to unpack (expected 2)
跟踪:user = Account.objects.get(username=request.POST['username'])
全面追溯:
File "/Users/eesamunir/Documents/Projects/youtube-project/accounts/views.py" in signup
28. user = Account.objects.get(username=request.POST['username'])
File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/query.py" in get
406. raise self.model.DoesNotExist(
During handling of the above exception (Account matching query does not exist.), another exception occurred:
File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/core/handlers/base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/core/handlers/base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/eesamunir/Documents/Projects/youtube-project/accounts/views.py" in signup
32. user = Account.objects.get(request.POST['email'])
File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/query.py" in get
399. clone = self.filter(*args, **kwargs)
File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/query.py" in filter
892. return self._filter_or_exclude(False, *args, **kwargs)
File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/query.py" in _filter_or_exclude
910. clone.query.add_q(Q(*args, **kwargs))
File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/sql/query.py" in add_q
1290. clause, _ = self._add_q(q_object, self.used_aliases)
File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/sql/query.py" in _add_q
1315. child_clause, needed_inner = self.build_filter(
File "/Users/eesamunir/.local/share/virtualenvs/youtube-project-snNRTgQy/lib/python3.8/site-packages/django/db/models/sql/query.py" in build_filter
1187. arg, value = filter_expr
Exception Type: ValueError at /accounts/signup/
Exception Value: too many values to unpack (expected 2)
有人有解决办法吗?谢谢。
答案 0 :(得分:3)
仔细阅读此行
File "/Users/eesamunir/Documents/Projects/youtube-project/accounts/views.py" in signup
32.user = Account.objects.get(request.POST['email'])
由于此而发生错误,
它应该是键值对Account.objects.get(email=request.POST['email'])