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

I got a UnicodeEncodeError when rendering a 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 an HTML snippet (standard use of forms):

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

I create them like this:

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

and sent to the 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 the first line, but in HTML I got

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

I found out that in the case of a simple string

 unicode(your_string) 

will be enough, but I don’t know how to use it in forms. Do I have to go through them and install some kind of filter?

EDIT: adding full trace:

 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) 
+6
source share
1 answer

Your __str__ model does not handle Unicode correctly. You will need to use explicit encoding:

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

or use the force_bytes() function :

 from django.utils.encoding import force_bytes def __str__(self): return force_bytes(self.name) 

The __str__ method should return a bytestring in Python 2.

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

 def __unicode__(self): return self.name 

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

+22
source

Source: https://habr.com/ru/post/980533/


All Articles