Django,send_mail,没有足够的值来解包

Posted

技术标签:

【中文标题】Django,send_mail,没有足够的值来解包【英文标题】:Django, send_mail, gets not enough values to unpack 【发布时间】:2021-09-12 23:27:48 【问题描述】:

,也试过了 EmailMultiAlternatives、EmailMessage 添加了回溯。下列 希望信息能帮助您努力寻找 解决方案。


            ValueError at /accounts/password_reset/
            not enough values to unpack (expected 2, got 1)
            Request Method: POST
            Request URL:    http://127.0.0.1:8000/accounts/password_reset/
            Django Version: 3.1
            Exception Type: ValueError
            Exception Value:    
            not enough values to unpack (expected 2, got 1)
            Exception Location: d:\Python\Code\dictionary\.env\lib\site-packages\django\core\mail\message.py, line 96, in sanitize_address
            Python Executable:  d:\Python\Code\dictionary\.env\Scripts\python.exe
            Python Version: 3.9.1

类 CustomResetPasswordView(PasswordResetView): def post(self, request, *args, **kwargs): email = request.POST.get('email') 尝试: _user = User.objects.filter(email=email).first() 如果不是 _user: raise Exception('无效的电子邮件地址!') 别的: 上下文 = '电子邮件' : _user.email, '域' : get_current_site(request).domain, 'uid' : urlsafe_base64_encode(force_bytes(_user.pk)), “用户”:_用户, 'token' : default_token_generator.make_token(_user), 'protocol': 'https' 如果 request.is_secure() 否则 'http',

               _superuser = User.objects.filter(is_superuser=True).values_list('email').first()
               send_mail(
                    subject='Password Reset Request',
                    message=context,
                    from_email=_superuser,
                    recipient_list=[_user.email]
                )
        except Exception as e:
            raise

settings:
    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_USE_TLS = True
    EMAIL_HOST = 'mail.smtp2go.com'
    EMAIL_HOST_USER = 'confidential'
    EMAIL_HOST_PASSWORD = 'confidential'

Environment:


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

Django Version: 3.1
Python Version: 3.9.6
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django_python3_ldap',
 'DictionaryApp.apps.DictionaryappConfig',
 'accounts.apps.AccountsConfig',
 'crispy_forms']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "D:\Python\Code\dictionary\.env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "D:\Python\Code\dictionary\.env\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\Python\Code\dictionary\.env\lib\site-packages\django\views\generic\base.py", line 73, in view
    return self.dispatch(request, *args, **kwargs)
  File "D:\Python\Code\dictionary\.env\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "D:\Python\Code\dictionary\.env\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "D:\Python\Code\dictionary\.env\lib\site-packages\django\contrib\auth\views.py", line 222, in dispatch
    return super().dispatch(*args, **kwargs)
  File "D:\Python\Code\dictionary\.env\lib\site-packages\django\views\generic\base.py", line 101, in dispatch
    return handler(request, *args, **kwargs)
  File "D:\Python\Code\dictionary\DictionaryApp\views.py", line 79, in post
    msg.send()
  File "D:\Python\Code\dictionary\.env\lib\site-packages\django\core\mail\message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "D:\Python\Code\dictionary\.env\lib\site-packages\django\core\mail\backends\smtp.py", line 109, in send_messages
    sent = self._send(message)
  File "D:\Python\Code\dictionary\.env\lib\site-packages\django\core\mail\backends\smtp.py", line 121, in _send
    from_email = sanitize_address(email_message.from_email, encoding)
  File "D:\Python\Code\dictionary\.env\lib\site-packages\django\core\mail\message.py", line 96, in sanitize_address
    nm, address = addr

Exception Type: ValueError at /accounts/password_reset/
Exception Value: not enough values to unpack (expected 2, got 1)




【问题讨论】:

请提供调试详情 【参考方案1】:

错误是由以下代码行引起的... _superuser = User.objects.filter(is_superuser=True).values_list('email').first()

我把它改成了 _superuser = User.objects.filter(is_superuser=True).first() _superuser = _superuser.email

_msg.send 期望电子邮件作为第一个参数,而不是像我最初那样的整个射击比赛。

【讨论】:

以上是关于Django,send_mail,没有足够的值来解包的主要内容,如果未能解决你的问题,请参考以下文章

ValueError:没有足够的值来解包(预期 3,得到 2)

python OpenCV中的ValueError - 没有足够的值来解包(预期3,得到2)

ValueError:没有足够的值来解包(预期 3,得到 2)。啥地方出了错? [复制]

Pandas read_excel 返回'没有足够的值来解包(预期 2,得到 1)'

Python 2 - ValueError:没有足够的值来解包(预期 6,得到 1)

ValueError:没有足够的值来解包(预期2,得1)