ValueError:Django 中的值太多无法解包

24 投票
6 回答
19741 浏览
提问于 2025-04-17 10:46

我刚刚把我的第一个Django应用程序部署好了。

我执行了一个 syncdb 命令,并为这个网站创建了一个超级用户账号。

现在当我访问页面并点击登录按钮时,出现了一个错误。我觉得这可能跟密码有关,但我不太确定。

ValueError at /accounts/login/
too many values to unpack

我正在使用通用的登录视图。

(r'^accounts/login/$', login, {'template_name': 'authentication/login.html'}),

下面是错误追踪信息:

Environment:


Request Method: POST
Request URL: http://xx.xx.xx.xx:8888/accounts/login/?next=/some_page/

Django Version: 1.3.1
Python Version: 2.7.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'bc_system_app',
 'django.contrib.humanize']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.middleware.csrf.CsrfResponseMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  93.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
  79.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/views.py" in login
  35.         if form.is_valid():
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in is_valid
  121.         return self.is_bound and not bool(self.errors)
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in _get_errors
  112.             self.full_clean()
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in full_clean
  268.         self._clean_form()
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in _clean_form
  296.             self.cleaned_data = self.clean()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/forms.py" in clean
  85.             self.user_cache = authenticate(username=username, password=password)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/__init__.py" in authenticate
  55.             user = backend.authenticate(**credentials)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/backends.py" in authenticate
  18.             if user.check_password(password):
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/models.py" in check_password
  275.         return check_password(raw_password, self.password)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/models.py" in check_password
  42.     algo, salt, hsh = enc_password.split('$')

Exception Type: ValueError at /accounts/login/
Exception Value: too many values to unpack

6 个回答

18

最简单的解决办法是通过命令行重置你的密码。

./manage.py changepassword <user>
33

我刚遇到同样的问题。

在我的情况下,网站是在Django 1.4版本下创建密码的(因为PYTHONPATH搞混了)。

当我尝试在1.3.1版本下登录时,出现了错误。然后我注意到Django的版本,切换到1.4后,登录又开始正常工作了。

看起来在1.4版本中,密码的加密算法发生了变化:

https://docs.djangoproject.com/en/dev/releases/1.4-beta-1/#improved-password-hashing

如果你使用的是Django 1.4的测试版,可能密码也会被损坏(请查看警告)。

11

是的,密码确实有问题。

问题出在密码的加密方式以及它在数据库中的存储。我们可以从错误信息中的这句话 algo, salt, hsh = enc_password.split('$') 看出来。这个加密后的密码在分割时返回的值超过了三个。

所以,请检查一下密码的加密方案和相关内容。

撰写回答