Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Unicode Django URL参数_Python_Django_Unicode - Fatal编程技术网

Python Unicode Django URL参数

Python Unicode Django URL参数,python,django,unicode,Python,Django,Unicode,我目前有以下模式: 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_

我目前有以下模式:

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'),
]
为用户名变量设置的值为:

u'k\xe5re%20j\xf8rgen'
上面应该说“kåre jø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)

URL不能包含unicode字符。浏览器应转义该字符串,并将其作为ascii发送。如果您的正则表达式不是unicode字符串,那么应该可以工作,不过当您获得name变量时,必须对其进行百分比解码


请发布完整的回溯。这似乎与您的url模式无关。回溯显示问题出在您的视图的这一行:
response=urllib2.urlopen('https://'+region+'.api.pvp.net/api/lol/'+region+'/v1.4/caller/by name/'+caller_name_-api+'?api_-key='+api_-key.\uu-str_u())
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)