2012-01-10 74 views
0

我正在使用谷歌地图http://py-googlemaps.sourceforge.net/显示一些旅行信息。我发送start_addressend_address通过窗体并计算路由服务器端。django:将unicode地址发送到Google地图

一切正常,当我使用的地址基本ASCII charaters,但如果我用野生克罗地亚的字符,如“čćšž”我得到“‘ASCII’编解码器不能编码位置字符U‘\ u010d’ ...“。

,如果我在shell中使用

from googlemaps import GoogleMaps 
directions = GoogleMaps().directions(smart_str(start_address), smart_str(end_address)) 

为命令运行良好,而不是当我在网站上通过测试服务器上运行。 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)) 

可以考虑的问题关闭:)

回答

0

ascii' codec can't encode character u'\u010d' in position...手段,Django的尝试从Unicode转换为Ascii。什么是settings.DEFAULT_CHARSET值?

尝试设置settings.DEFAULT_CHARSET = 'UTF-8'

+0

ouh,它没有设置: - /但现在我得到_'ascii'编解码器无法解码位置的字节0xc4 ... _ – mislavcimpersak 2012-01-10 14:53:40

+0

请发送一个完整的(“工作”)代码与文本这不起作用。 – 2012-01-11 06:39:14

+0

感谢Alexander Artemenko的帮助。得到它的工作,并张贴我的完整工作代码在原来的职位:) – mislavcimpersak 2012-01-11 07:31:51