How to receive variables in a post in google app e

2019-01-27 03:49发布

email = self.request.get('email')
name = self.request.get('name')
mail.send_mail(sender="myemail", email=email, body=name, subject="sss " + name + "sdafsaã")

// added ã: the problem was that "sdafsaã" should be u"sdafsaã". with a "u" before the string. and now it works

then i get this

main.py", line 85, in post

subject="sss " + name + "sdafsa",
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 36: ordinal not in range(128)

the might have chars like õ ó and something like that.

for more details:

the code to run the worker(the code before) the name is the one that is received from the datastore and contains chars like õ and ó...

 taskqueue.add(url='/emailworker', params={'email': e.email, 'name': e.name})

thanks

4条回答
Emotional °昔
2楼-- · 2019-01-27 03:55

the problem is joining 2 strings: ||| body = name + "ã" => error ||| body = name + u"ã" => works!!! |||

查看更多
女痞
3楼-- · 2019-01-27 04:02

Try with encode

t ='việt ứng '
m = MyModel()
m.data = t.encode('utf-8')
m.put() #success!
查看更多
可以哭但决不认输i
4楼-- · 2019-01-27 04:11

Try reading a little about how unicode works in Python:

Also, make sure you're running Python 2.5 if you are seeing this error on the development server.

查看更多
Emotional °昔
5楼-- · 2019-01-27 04:11

You should use:

email = self.request.get('email')
name = self.request.get('name')
mail.send_mail(sender="myemail", 
               email=email, 
               body=name, 
               subject="hello " + name.encode('utf-8') + " user!")

The variable name is a unicode string and should encoded in utf-8 or in the kind of encode you are using in you web application before concatenating to other byte strings.
Without name.encode(), Python uses the default 7 bits ascii codec that can't encode that specific character.

查看更多
登录 后发表回答