Python 2.7, Appengine Data Store & Unicode

2019-06-27 09:09发布

So I've been reading quite a bit about Unicoding tonight because I was thinking of switching to Jinja2, which requires Unicode to be used everywhere within the app. I think I have a good idea of how to deal with it, but I wanted to hear if this is reasonable before I started to code my app:

  1. Dealing with External Text-Inputs (via html forms)

    a) Make sure all html pages are utf-8 encoded.
    b) Once users press submit, make sure the data is converted into Unicode as soon as the python backend receives it...decode(self.request.get('stuff'),utf-8)
    c) Stay in unicode, transfer the outputs to Jinja2 which will always it using the default encoding of utf-8.

  2. Information from the appengine datastore

    Because google stores everything as Unicode, all data coming in from the datastore is already unicode and I don't have to worry about anything (yay!)

  3. Strings within the app

    Make sure all "" start with a u (i.e. u"hello world"), this will force everything to be in unicode.

Well the above is my strategy to keep everything consistent. Is there anything else I need to account for?

thanks!

1条回答
放我归山
2楼-- · 2019-06-27 09:45

You should not need to .decode(self.request.get('stuff'),utf-8 if you using webapp or webapp2. The framework respects the input type of the data as specified.

Everything else looks right.

Also I believe that

from __future__ import unicode_strings

should be

from __future__ import unicode_literals

and is only available in 2.6 and 2.7 So in App Engine it would only be available if you are using 2.7

查看更多
登录 后发表回答