Greg
Greg

Reputation: 47094

Unicode Error in urllib.urlencode - in my Django Web App

\Hi All,

A user is entering a unicode value (u'\u2206') into a text box that is submitted to my django view. The view uses the GET data to generate a URL to get back to the search page later.

It's having trouble generating the URL when the user submits a unicode charactor.

What is the best practice here. Should I encode the get data before sending it into urlencode? What encoding should I use?

Relevant code:

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())

Error: Traceback (most recent call last):

  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)

Upvotes: 2

Views: 1425

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

Encode as UTF-8 before URL-encoding.

Upvotes: 6

Related Questions