我的错误是什么意思?

时间:2011-12-05 02:02:56

标签: python debugging google-app-engine internationalization

我正在尝试使用新密码发送本地化电子邮件。当我尝试发送邮件时似乎崩溃了:

Traceback (most recent call last):
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 174, in Handle
    result = handler(self._environ, self._StartResponse)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1519, in __call__
    response = self._internal_error(e)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
    return handler.dispatch()
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~montaoproject/newhome.355152717011517235/main.py", line 1962, in post
    message.send()
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/mail.py", line 887, in send
    message = self.ToProto()
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/mail.py", line 1122, in ToProto
    message = super(EmailMessage, self).ToProto()
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/mail.py", line 830, in ToProto
    message.set_subject(_to_str(self.subject))
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/_internal/proto1/internal/cpp_message.py", line 91, in Setter
    self._cmsg.SetScalar(cdescriptor, value)
TypeError: <class 'django.utils.functional.__proxy__'> has type <class 'django.utils.functional.__proxy__'>, but expected one of: str, unicode

我的代码是:

def post(self, key):
    ad = db.get(db.Key(key))
    if not ad:  # or not ad.published:
        self.error(404)
        return
    email = self.request.POST['email']
    if ad.email == email or ad.user.email() == email:
        size = 9
        vowels = 'aeiou'
        consonants = 'bcdfghjklmnpqrstvwxyz'
        password = ''
        from random import randint
        from random import choice
        import random
        minpairs = 4
        maxpairs = 6
        for x in range(1, random.randint(int(minpairs),
                       int(maxpairs))):
            consonant = consonants[random.randint(1,
                    len(consonants) - 1)]
            if random.choice([1, 0]):
                consonant = string.upper(consonant)
            password = password + consonant
            vowel = vowels[random.randint(1, len(vowels) - 1)]
            if random.choice([1, 0]):
                vowel = string.upper(vowel)
            password = password + vowel
            newpasswd = password
        ad.set_password(newpasswd)
        ad.save()
        url = (os.environ['HTTP_HOST'] if os.environ.get('HTTP_HOST'
               ) else os.environ['SERVER_NAME'])
        if users.get_current_user():
            senderemail = users.get_current_user().email()
        elif url.endswith('.br'):
            senderemail = 'info@montao.com.br'
            translation.activate('pt-br')
        else:
            senderemail = 'admin@koolbusiness.com'
        message = mail.EmailMessage(sender=senderemail,
                subject=_('Password reset successful'))
        if ad.user:
            message.to = ad.user.email()
        else:
            message.to = ad.email
        to = message.to
        output = \
            _("You're receiving this e-mail because you requested a password reset"
              ) + ' ' + _('Your new password is: %(new_password)s') \
            % {'new_password': newpasswd}
        msg = output
        if isinstance(msg, unicode):
            msg = msg.encode('utf-8')
        message.body = '%s %s/%d' % (msg, url, ad.key().id())
        message.send()
        self.response.out.write(_("We've e-mailed a new password to the e-mail address you submitted. You should be receiving it shortly."
                                ))
    else:
        self.response.out.write('unknown email ')

你能告诉我应该改变什么来使代码工作吗?它曾经工作,但现在我已经改变了翻译的加载方式,其他页面和功能都有效,所以我希望这样做也可以。

由于

1 个答案:

答案 0 :(得分:4)

看起来你的翻译机制“_”正在返回翻译价值的代理。但是,message.send()正在检查类型并期望一个字符串。试试这个改变:

改变这个:

message = mail.EmailMessage(sender=senderemail,
        subject=_('Password reset successful'))

为:

message = mail.EmailMessage(sender=senderemail,
        subject=unicode(_('Password reset successful')))

这应该强制你的代理咳出一个真正的字符串并让message.send()开心。