\大家好,
用户正在将unicode值(u'\ u2206')输入到提交到我的django视图的文本框中。该视图使用GET数据生成URL以便稍后返回搜索页面。
当用户提交unicode字符时,无法生成URL。
这里的最佳做法是什么?我应该在将数据发送到urlencode之前对其进行编码吗?我应该使用什么编码?
相关代码:
search_form = SearchForm(request.GET)
get_data = dict([(key,item) for (key,item) in search_form.data.items()])
get_params = '&' + urllib.urlencode(query=get_data.items())
错误: 回溯(最近一次调用最后一次):
File "/opt/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 100, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/opt/python/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 25, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/opt/DjangoProjects/Fly/FlyStore/views.py", line 465, in search
get_params = '&' + urllib.urlencode(query=get_data.items())
File "/opt/python/lib/python2.7/urllib.py", line 1282, in urlencode
v = quote_plus(str(v))
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2206' in position 15: ordinal not in range(128)
答案 0 :(得分:6)
在网址编码之前编码为UTF-8。