【问题标题】:django: sending unicode address to googmaps from formdjango:从表单向 googmaps 发送 unicode 地址
【发布时间】:2012-01-10 14:41:42
【问题描述】:

我正在使用 googlemaps http://py-googlemaps.sourceforge.net/ 显示一些旅行信息。我通过表单发送 start_addressend_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


    【解决方案1】:

    ascii' codec can't encode character u'\u010d' in position... 表示,django 尝试从 Unicode 转换为 Ascii。你的settings.DEFAULT_CHARSET 值是多少?

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

    【讨论】:

    • 哦,它没有设置 :-/ 但现在我得到 'ascii' 编解码器无法解码位置中的字节 0xc4 ...
    • 请发布一个完整的(“工作”)代码,其中包含无效的文本。
    • 感谢 Alexander Artemenko 的帮助。让它工作并在原始帖子中发布我的完整工作代码:)
    猜你喜欢
    • 1970-01-01
    • 2016-01-19
    • 2015-05-10
    • 2014-08-04
    • 2016-08-17
    • 2016-10-04
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    相关资源
    最近更新 更多