我是金字塔的新手。我使用python2.7,MSSQL,slqalchemy.mssql并使用gbk
字符集,我有很多旧数据。所以我使用以下代码将前面的html更改为gbk
:
@view_config(route_name='g', renderer='json')
def my_view1(request):
print 2
print request.charset
print 1
print isinstance(request.params['one'], str)
if request.params['one']:
print request.params['one']
filters = (Bzjl.one == request.params['one'])
try:
two1 = DBSession.query(Bzjl).filter(filters)
except DBAPIError:
return Response(conn_err_msg, content_type='text/plain', status_int=500)
return Response(getjson(two1))
示例网址:http://127.0.0.1:6543/g?one='中国福建'
不返回任何行:{"records": 0, "total": 20, "rows": [], "page": 1}
答案 0 :(得分:4)
Pyramid支持请求工厂。您可以使用它来解码请求。
def request_factory(environ):
req = pyramid.request.Request(environ)
return req.decode(charset='gbk')
config.set_request_factory(request_factory)
这意味着在金字塔内传递的请求将使用gbk字符集。无法使用此charset解码请求将导致UnicodeDecodeError异常,因此您可能希望向请求工厂添加额外的逻辑来处理这些事情。