2011-12-05 154 views
1

我在嘗試使用新密碼發送本地化電子郵件。這似乎崩潰時,我嘗試發送郵件:我的錯誤是什麼意思?

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 = '[email protected]' 
      translation.activate('pt-br') 
     else: 
      senderemail = '[email protected]' 
     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 ') 

你能告訴我,我應該改變,以使代碼工作?它曾經工作,但現在我已經改變了翻譯的方式加載,其他頁面和功能的工作,所以我希望這也做到了。

謝謝

回答

4

看起來你的翻譯機制「_」正在返回翻譯值的代理。但是,message.send()正在檢查類型並期望一個字符串。試試這個變化:

更改此:

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

到:

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

這應該強制代理咳出一個真正的字符串,使message.send()高興。

+0

它的工作原理。非常感謝。 –