2

I currently have this 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'),
]

however when the URL has a utf-8 character like 'å', it gives me an error:

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

The value getting set for the username var is:

u'k\xe5re%20j\xf8rgen'

The above is supposed to say 'kåre jørgen'.

The full traceback:

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)
3
  • 1
    Please post the full traceback. Commented Feb 11, 2016 at 23:54
  • dpaste.com/3XS44GQ Commented Feb 11, 2016 at 23:56
  • 4
    This doesn't appear to have anything to do with your url patterns. The traceback shows that the problem is on this line of your view: response = urllib2.urlopen('https://' + region + '.api.pvp.net/api/lol/' + region + '/v1.4/summoner/by-name/' + summoner_name_api + '?api_key=' + api_key.__str__()) Commented Feb 12, 2016 at 0:12

1 Answer 1

3

URLs cannot contain unicode characters. The browser should percent-escape the string, and send it as ascii. Things should work if your regex is not a unicode string, though when you get your name variable, you will have to percent-decode it.

Unicode characters in URLs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.