我目前有这个urlpattern:
from __future__ import unicode_literals
from django.confs.urls import url
from django.utils.encoding import python_2_unicode_compatible
from . import views
app_name = 'bans'
urlpatterns = [
url(ur'^(?P<region>.*)/(?P<summoner_name>.*)/$', views.get_user, name='bans'),
]
但是当网址有'å'这样的utf-8字符时,它会给我一个错误:
'ascii' codec can't encode character u'\xe5' in position 40: ordinal not in range(128)
为用户名var设置的值是:
u'k\xe5re%20j\xf8rgen'
上面应该说'kårejørgen'。
完整的追溯:
Environment:
Request Method: GET
Request URL: http://localhost:8000/bans/euw/k%C3%A5re%20j%C3%B8rgen/
Django Version: 1.9.2
Python Version: 2.7.10
Installed Applications:
['bans.apps.BansConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/petter/Documents/Web projects/leaguebans django/leaguebans/bans/views.py" in get_bans
25. response = urllib2.urlopen('https://' + region + '.api.pvp.net/api/lol/' + region + '/v1.4/summoner/by-name/' + summoner_name_api + '?api_key=' + api_key.__str__())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in urlopen
154. return opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in open
431. response = self._open(req, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in _open
449. '_open', req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in _call_chain
409. result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in https_open
1240. context=self._context)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in do_open
1194. h.request(req.get_method(), req.get_selector(), req.data, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py" in request
1053. self._send_request(method, url, body, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py" in _send_request
1093. self.endheaders(body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py" in endheaders
1049. self._send_output(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py" in _send_output
893. self.send(msg)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py" in send
869. self.sock.sendall(data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py" in sendall
721. v = self.send(data[count:])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py" in send
687. v = self._sslobj.write(data)
Exception Type: UnicodeEncodeError at /bans/euw/kåre jørgen/
Exception Value: 'ascii' codec can't encode character u'\xe5' in position 40: ordinal not in range(128)
答案 0 :(得分:1)
网址不能包含unicode字符。浏览器应该百分比转义字符串,并将其作为ascii发送。如果你的正则表达式不是unicode字符串,事情应该有效,但是当你得到你的名字变量时,你将不得不对它进行百分比解码。