2016-02-11 38 views
0

我現在有這個URLPATTERN:Unicode的Django的URL參數

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'), 
] 

然而,當URL有一個像 'A' 向UTF-8字符,它給了我一個錯誤:

'ascii' codec can't encode character u'\xe5' in position 40: ordinal not in range(128) 

值越來越設定的用戶名變種是:

u'k\xe5re%20j\xf8rgen' 

以上應該說「卡蕾約爾根」。

完整回溯:

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) 
+1

請發表完整的回溯。 –

+0

http://dpaste.com/3XS44GQ – Gopi

+4

這似乎與您的網址格式沒有任何關係。追溯顯示問題出現在您的視圖的這一行上:'response = urllib2.urlopen('https://'+ region +'.api.pvp.net/api/lol /'+ region +'/ v1。 4/summoner/by-name /'+ summoner_name_api +'?api_key ='+ api_key .__ str __())' – Alasdair

回答

1

網址不能包含Unicode字符。瀏覽器應該百分比轉義字符串,並將其作爲ascii發送。如果你的正則表達式不是一個unicode字符串,事情應該會起作用,儘管當你得到你的名字變量時,你必須對它進行百分比解碼。

Unicode characters in URLs