Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django UnicodeEncodeError in rendering form ('utf-8')

I got an UnicodeEncodeError while rendering page using forms.

UnicodeEncodeError at /individual/acc/

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

Here's fragment of HTML (standard use of forms):

<table>
    {{user_form.as_table}}
    {{individual_form.as_table}}
</table>

In view I create them like that:

user_form = UserCreationForm(request.POST)
individual_form = IndividualForm(request.POST)

and sent to page

context = {'gd': generalData(request),
           'head': "images/header_individual.png",
           'individual': user.individual,
           'user_form': user_form,
           'individual_form': individual_form,
           'title': _("EDIT ACCOUNT"), }
return render(request,
              "main/edit_account.html",
              context)

In all my .py files I have

# -*- coding: utf-8 -*-

in first line, and in HTML I got

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

I found out that in case of simple string

unicode(your_string)

would be enough, but I don't really know how to use it in forms. Should I iterate through them and set some kind of filter?

EDIT: adding full traceback:

UnicodeEncodeError at /individual/acc/

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

Request Method:     GET
Request URL:    http://127.0.0.1:8080/individual/acc/
Django Version:     1.7.1
Exception Type:     UnicodeEncodeError
Exception Value:    

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

Exception Location:     /home/mknapczyk/Workspace/ASPHER/src/main/models.py in __str__, line 42
Python Executable:  /home/mknapczyk/Workspace/ASPHER/env/bin/python
Python Version:     2.7.6
Python Path:    

['/home/mknapczyk/Workspace/ASPHER/src',
 '/home/mknapczyk/Workspace/ASPHER/env/lib/python2.7',
 '/home/mknapczyk/Workspace/ASPHER/env/lib/python2.7/plat-x86_64-linux-gnu',
 '/home/mknapczyk/Workspace/ASPHER/env/lib/python2.7/lib-tk',
 '/home/mknapczyk/Workspace/ASPHER/env/lib/python2.7/lib-old',
 '/home/mknapczyk/Workspace/ASPHER/env/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages',
 '/home/mknapczyk/Workspace/ASPHER/env/lib/python2.7/site-packages']

Server time:    Mon, 5 Jan 2015 14:11:25 +0000
Unicode error hint

The string that could not be encoded/decoded was: Małopols

Traceback:

File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/home/mknapczyk/Workspace/ASPHER/src/main/views.py" in edit_account
  105.                context)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/shortcuts.py" in render
  48.     return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  178.         return t.render(context_instance)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  148.             return self._render(context)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  126.         return compiled_parent._render(context)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  65.                 result = block.nodelist.render(context)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/debug.py" in render
  90.             output = self.filter_expression.resolve(context)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/base.py" in resolve
  596.                 obj = self.var.resolve(context)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/base.py" in resolve
  734.             value = self._resolve_lookup(context)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/template/base.py" in _resolve_lookup
  788.                             current = current()
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/forms/forms.py" in as_table
  257.             errors_on_separate_row=False)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/forms/forms.py" in _html_output
  219.                     'field': six.text_type(bf),
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/forms/forms.py" in __str__
  508.         return self.as_widget()
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/forms/forms.py" in as_widget
  560.         return force_text(widget.render(name, self.value(), attrs=attrs))
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/forms/widgets.py" in render
  504.         options = self.render_options(choices, [value])
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/forms/widgets.py" in render_options
  530.         for option_value, option_label in chain(self.choices, choices):
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/forms/models.py" in __iter__
  1083.                 yield self.choice(obj)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/forms/models.py" in choice
  1090.         return (self.field.prepare_value(obj), self.field.label_from_instance(obj))
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/forms/models.py" in label_from_instance
  1144.         return smart_text(obj)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/utils/encoding.py" in smart_text
  52.     return force_text(s, encoding, strings_only, errors)
File "/home/mknapczyk/Workspace/ASPHER/env/local/lib/python2.7/site-packages/django/utils/encoding.py" in force_text
  87.                 s = six.text_type(bytes(s), encoding, errors)
File "/home/mknapczyk/Workspace/ASPHER/src/main/models.py" in __str__
  42.       return str(self.name)

Exception Type: UnicodeEncodeError at /individual/acc/
Exception Value: 'ascii' codec can't encode character u'\u0142' in position 2: ordinal not in range(128)
like image 306
Mateusz Knapczyk Avatar asked Jan 05 '15 13:01

Mateusz Knapczyk


People also ask

How do I fix UnicodeEncodeError in Python?

Only a limited number of Unicode characters are mapped to strings. Thus, any character that is not-represented / mapped will cause the encoding to fail and raise UnicodeEncodeError. To avoid this error use the encode( utf-8 ) and decode( utf-8 ) functions accordingly in your code.

What is Unicode in Django?

If you define a __unicode__() method, Django will call it when it needs to render an object in a context where a string representation is needed (e.g. in the model's admin pages). The documentation says: The __unicode__() method is called whenever you call unicode() on an object.

What are templates in Django?

A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags. A template is rendered with a context.


1 Answers

Your model __str__ is not handling Unicode correctly. You'll need to use explicit encoding:

def __str__(self):
    return self.name.encode('utf8')

or use the force_bytes() utility function:

from django.utils.encoding import force_bytes

def __str__(self):
    return force_bytes(self.name)

The __str__ method must return a bytestring in Python 2.

You probably want to add a __unicode__ method for your model to avoid this issue in the first place:

def __unicode__(self):
    return self.name

Also see Python __str__ versus __unicode__, as well as the Model.__unicode__ reference documentation.

like image 101
Martijn Pieters Avatar answered Sep 25 '22 15:09

Martijn Pieters