【发布时间】:2012-01-10 14:41:42
【问题描述】:
我正在使用 googlemaps http://py-googlemaps.sourceforge.net/ 显示一些旅行信息。我通过表单发送 start_address 和 end_address 并计算路由服务器端。
当我在地址中使用基本的 ascii 字符时一切正常,但如果我使用诸如 'čćšž' 之类的野生克罗地亚字符,我会得到 "'ascii' 编解码器无法编码字符 u '\u010d' 在位置..."。
如果我使用
from googlemaps import GoogleMaps
directions = GoogleMaps().directions(smart_str(start_address), smart_str(end_address))
作为命令在 shell 中运行良好,但当我通过网站上的测试服务器运行时却不行。 start_address 和 end_address 都是 unicode 类型。
那么我应该如何形成 start_address 以使其与整个 unicode 一起正常运行?
编辑:
折腾了一些之后,这就是最终起作用的代码:
from django.shortcuts import render_to_response
from django.utils.encoding import smart_unicode, smart_str
from googlemaps import GoogleMaps
def calculations(request):
if request.method == 'POST':
trip = {}
start_address = smart_str(request.POST.get('start_address'))
end_address = smart_str(request.POST.get('end_address'))
directions = GoogleMaps().directions(start_address, end_address)
trip['length'] = directions['Directions']['Distance']['html']
trip['duration'] = directions['Directions']['Duration']['html']
return render_to_response( 'index.html', {'trip':trip, }, context_instance = RequestContext(request) )
你可以考虑关闭问题:)
【问题讨论】:
标签: python django google-maps unicode