为整个应用程序设置Python27 Google AppEngine默认编码 [英] Set Python27 Google AppEngine default encoding for entire app

查看:164
本文介绍了为整个应用程序设置Python27 Google AppEngine默认编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的python27 appengine站点设置默认编码为utf-8。默认值为ascii。

I would like to set the default encoding to utf-8 for my python27 appengine site. The default is ascii.

有一个类似的问题回答了 http://code.google.com/p/googleappengine/issues/detail?id=5923 。它说在设置默认编码后不要使用sys.reload,否则将丢失一个请求。

There was a similar question answered http://code.google.com/p/googleappengine/issues/detail?id=5923. It says to not use sys.reload after setting the default encoding or you will lose a request.

如何为我的整个python appengine站点设置utf-8编码,而不必像上面提到的链接那样编码字符串?

How can i set utf-8 encoding for my entire python appengine site, without having to encode strings specifically like the link suggests above?

感谢任何帮助。

推荐答案

您可以启动您的python 27代码(每个Python文件) :

You can start your python 27 code (every Python file) with:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

但有时你必须使用.encode('ascii')if您使用HMAC或您必须设置http标头。
或者你可以使用:

But sometimes you have to use .encode('ascii') if you use HMAC or you have to set http headers. Or you can use:

self.response.headers[str('Content-Type')] = str(content_type)

 self.response.headers[b'Content-Type'] = str(content_type)

请确保:


  • 所有HTML文件都使用UTF-8

  • 您的编辑器默认使用UTF-8

这篇关于为整个应用程序设置Python27 Google AppEngine默认编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆