关于mysql:渲染时捕获UnicodeEncodeError:’ascii’编解码器不能编码位置4-5中的字符:序数不在范围内(128)

Caught UnicodeEncodeError while rendering: 'ascii' codec can't encode characters in position 4-5: ordinal not in range(128)

你好,我住在波兰,所以我必须处理像这样的信件???等。当我试图在管理面板中添加"zesp_"之类的文本时。SZK?"我得到这样的错误:

Caught UnicodeEncodeError while rendering: 'ascii' codec can't encode
characters in position 4-5: ordinal not in range(128)

我在StackOverflow页面上看到许多类似的问题,但我不知道如何解决。

我的数据库是mysql

我的数据库的unicode集合:utf8_unicode_ci

我的模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- coding: utf-8 -*-
from django.db import models
import codecs

...    

class experience(models.Model):
    dateStart = models.DateField()
    dateEnd = models.DateField()
    company = models.CharField(max_length=50)
    position = models.CharField(max_length=50)
    description = models.TextField(max_length=255)
    def __unicode__(self):
        return self.company.decode('utf8')    

...

追溯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
Request Method: GET
Request URL: http://vm:8000/admin/mycv/experience/

Django Version: 1.3.1
Python Version: 2.6.6
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'cv.mycv']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Template error:
In template /usr/local/lib/python2.6/dist-packages/django/contrib/admin/templates/admin/change_list.html, error at line 97
   Caught UnicodeEncodeError while rendering: 'ascii' codec can't encode characters in position 4-5: ordinal not in range(128)
   87 :         {% endif %}


   88 :       {% endblock %}


   89 :


   90 :       <form id="changelist-form" action="" method="post"{% if cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %}>{% csrf_token %}


   91 :       {% if cl.formset %}


   92 :         {{ cl.formset.management_form }}


   93 :       {% endif %}


   94 :


   95 :       {% block result_list %}


   96 :           {% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}


   97 :            {% result_list cl %}


   98 :           {% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}


   99 :       {% endblock %}


   100 :       {% block pagination %}{% pagination cl %}{% endblock %}


   101 :       </form>


   102 :    


   103 :  


   104 : {% endblock %}


   105 :

Traceback:
File"/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File"/usr/local/lib/python2.6/dist-packages/django/contrib/admin/options.py" in wrapper
  307.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File"/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in _wrapped_view
  93.                     response = view_func(request, *args, **kwargs)
File"/usr/local/lib/python2.6/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
  79.         response = view_func(request, *args, **kwargs)
File"/usr/local/lib/python2.6/dist-packages/django/contrib/admin/sites.py" in inner
  197.             return view(request, *args, **kwargs)
File"/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in _wrapper
  28.             return bound_func(*args, **kwargs)
File"/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in _wrapped_view
  93.                     response = view_func(request, *args, **kwargs)
File"/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in bound_func
  24.                 return func(self, *args2, **kwargs2)
File"/usr/local/lib/python2.6/dist-packages/django/contrib/admin/options.py" in changelist_view
  1179.         ], context, context_instance=context_instance)
File"/usr/local/lib/python2.6/dist-packages/django/shortcuts/__init__.py" in render_to_response
  20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File"/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in render_to_string
  188.         return t.render(context_instance)
File"/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render
  123.             return self._render(context)
File"/usr/local/lib/python2.6/dist-packages/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File"/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File"/usr/local/lib/python2.6/dist-packages/django/template/debug.py" in render_node
  73.             result = node.render(context)
File"/usr/local/lib/python2.6/dist-packages/django/template/loader_tags.py" in render
  127.         return compiled_parent._render(context)
File"/usr/local/lib/python2.6/dist-packages/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File"/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File"/usr/local/lib/python2.6/dist-packages/django/template/debug.py" in render_node
  73.             result = node.render(context)
File"/usr/local/lib/python2.6/dist-packages/django/template/loader_tags.py" in render
  127.         return compiled_parent._render(context)
File"/usr/local/lib/python2.6/dist-packages/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File"/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File"/usr/local/lib/python2.6/dist-packages/django/template/debug.py" in render_node
  73.             result = node.render(context)
File"/usr/local/lib/python2.6/dist-packages/django/template/loader_tags.py" in render
  64.             result = block.nodelist.render(context)
File"/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File"/usr/local/lib/python2.6/dist-packages/django/template/debug.py" in render_node
  73.             result = node.render(context)
File"/usr/local/lib/python2.6/dist-packages/django/template/loader_tags.py" in render
  64.             result = block.nodelist.render(context)
File"/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File"/usr/local/lib/python2.6/dist-packages/django/template/debug.py" in render_node
  73.             result = node.render(context)
File"/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render
  921.                     dict = func(*args)
File"/usr/local/lib/python2.6/dist-packages/django/contrib/admin/templatetags/admin_list.py" in result_list
  232.             '
results': list(results(cl))}
File"/usr/local/lib/python2.6/dist-packages/django/contrib/admin/templatetags/admin_list.py" in results
  217.             yield ResultList(None, items_for_result(cl, res, None))
File"/usr/local/lib/python2.6/dist-packages/django/contrib/admin/templatetags/admin_list.py" in __init__
  209.         super(ResultList, self).__init__(*items)
File"/usr/local/lib/python2.6/dist-packages/django/contrib/admin/templatetags/admin_list.py" in items_for_result
  137.             f, attr, value = lookup_field(field_name, result, cl.model_admin)
File"/usr/local/lib/python2.6/dist-packages/django/contrib/admin/util.py" in lookup_field
  218.                 value = attr()
File"/usr/local/lib/python2.6/dist-packages/django/db/models/base.py" in __str__
  377.             return force_unicode(self).encode('
utf-8')
File"/usr/local/lib/python2.6/dist-packages/django/utils/encoding.py" in force_unicode
  71.                 s = unicode(s)
File"/home/lechu/apps/cv/../cv/mycv/models.py" in __unicode__
  31.         return self.company.decode('
utf8')    
File"/usr/lib/python2.6/encodings/utf_8.py" in decode
  16.     return codecs.utf_8_decode(input, errors, True)

Exception Type: TemplateSyntaxError at /admin/mycv/experience/
Exception Value: Caught UnicodeEncodeError while rendering: '
ascii' codec can't encode characters in position 4-5: ordinal not in range(128)




   104 : {% endblock %}


   105 :

Traceback:
File

不幸的是,你的__unicode__方法是无稽之谈。

decode用于将字节串转换为unicode,但self.company已经是unicode了。因此,在上面调用decode('utf-8')意味着python必须先隐式地将其编码为bytestring,这是默认的ascii编码——一旦有重音字符,它就会失败。

我不知道您试图用这个方法做什么,但是__unicode__方法应该总是返回unicode。在这里,正确的做法是只使用return self.company