Sím*_*son 13 python google-app-engine character-encoding
我有一个简单的表单,提交图像到blobstore和图像的标题.这适用于我的本地devserver但是当我部署我的代码时,标题中的非ascii字母变成了乱码,带有ascii和hex的某种混合.例如,Ísland成为= CDsland.注意,我<meta http-equiv="Content-Type" content="text/html; charset=utf-8">在标题中使用 第一个值.此外,utf-8适用于我的所有其他形式.只是多部分形式变得乱码.无论如何这是我的形式:
<form action="{{ uploadurl }}" enctype="multipart/form-data" method="post">
<div><label>Title</label><input type="text" name="title" class="string" /></div>
<div><label>Picture</label><input type="file" name="img"/></div>
<div style="margin-top:10px;"><input type="submit" value="Add picture" /></div>
<input type="hidden" value="{{ album.key }}" name="alid"/>
</form>
Run Code Online (Sandbox Code Playgroud)
这是处理表单的类:
# handler for posting photos
class PostPhoto(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('img')
photourl = images.get_serving_url(str(upload_files[0].key()))
photo = Photo()
#because of multipart/form-data
photo.title = self.request.get("title")
photo.photourl = photourl
photo.photoalbum = PhotoAlbum.get(self.request.get('alid'))
photo.put()
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个问题?我是否必须进行一些服务器端编码/解码?我试过google搜索没有结果(python newb),所以这是我最后的手段,我只是改变我的设计并拆分表格.
这是一个已知的错误.http://code.google.com/p/googleappengine/issues/detail?id=3761
回到原始数据是一个问题:
>>> import quopri
>>> t = unicode(quopri.decodestring('=CD'), 'iso_8859-2')
>>> print t
Í
Run Code Online (Sandbox Code Playgroud)
我正在使用Django nonrel并使用此中间件修复它:
http://code.google.com/p/googleappengine/issues/detail?id=2749#c33
import logging
import quopri
log = logging.getLogger(__name__)
class BlobRedirectFixMiddleware(object):
def process_request(self, request):
if request.method == 'POST' and 'HTTP_X_APPENGINE_BLOBUPLOAD' in request.META and request.META['HTTP_X_APPENGINE_BLOBUPLOAD'] == 'true':
request.POST = request.POST.copy()
log.info('POST before decoding: %s' % request.POST)
for key in request.POST:
if key.startswith('_') or key == u'csrfmiddlewaretoken':
continue
value = request.POST[key]
if isinstance(value,(str, unicode)):
request.POST[key] = unicode(quopri.decodestring(value), 'iso_8859-2')
log.info('POST after decoding: %s' % request.POST)
return None
Run Code Online (Sandbox Code Playgroud)
小智 1
你试过 photourl = images.get_serving_url(unicode
(upload_files[0].key())) 插入 photourl = images.get_serving_url(str(upload_files[0].key()))
| 归档时间: |
|
| 查看次数: |
2192 次 |
| 最近记录: |