呈现形式中的 Django UnicodeEncodeError ('utf-8')

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

我在使用表单呈现页面时遇到 UnicodeEncodeError。

UnicodeEncodeError at /individual/acc/

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

这是 HTML 的片段(表格的标准用法):

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

鉴于我是这样创建它们的:

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

并发送到页面

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)

在我所有的 .py 文件中

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

在第一行,在HTML我得到了

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

我发现在简单字符串的情况下

unicode(your_string)

就足够了,但我真的不知道如何在表单中使用它。我应该遍历它们并设置某种过滤器吗?

编辑:添加完整回溯:

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

回溯:

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)

您的模型 __str__ 没有正确处理 Unicode。您需要使用显式编码:

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

或使用 force_bytes() utility function:

from django.utils.encoding import force_bytes

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

__str__方法必须returnPython2.

中的字节串

您可能想为您的模型添加一个 __unicode__ 方法来首先避免这个问题:

def __unicode__(self):
    return self.name

另见 Python __str__ versus __unicode__, as well as the Model.__unicode__ reference documentation