Django:/ admin / structure / type / add /中的UnicodeEncodeError

时间:2017-11-02 11:28:15

标签: python django

输入带有这些法语字符的字符串时出现问题:é,è,à等等......

我尝试了两件事:

第一件事是在我的views.py文件的最顶部添加:# -*- coding: utf-8 -*- 第二件事是将它添加到我的settings.py文件:DEFAULT_CHARSET = 'utf-8'

每当我输入带有特殊字符的字符串时,我仍会收到此错误消息:

Environment:


Request Method: POST
Request URL: http://10.0.0.238:8000/admin/structure/type/add/?_to_field=id&_popup=1

Django Version: 1.8
Python Version: 2.7.13
Installed Applications:
('apps.structure',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/home/kaiss/.local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/kaiss/.local/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  616.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/kaiss/.local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "/home/kaiss/.local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "/home/kaiss/.local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  233.             return view(request, *args, **kwargs)
File "/home/kaiss/.local/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
  1516.         return self.changeform_view(request, None, form_url, extra_context)
File "/home/kaiss/.local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  34.             return bound_func(*args, **kwargs)
File "/home/kaiss/.local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "/home/kaiss/.local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  30.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/kaiss/.local/lib/python2.7/site-packages/django/utils/decorators.py" in inner
  145.                     return func(*args, **kwargs)
File "/home/kaiss/.local/lib/python2.7/site-packages/django/contrib/admin/options.py" in changeform_view
  1470.                     self.log_addition(request, new_object)
File "/home/kaiss/.local/lib/python2.7/site-packages/django/contrib/admin/options.py" in log_addition
  810.             object_repr=force_text(object),
File "/home/kaiss/.local/lib/python2.7/site-packages/django/utils/encoding.py" in force_text
  94.                 s = six.text_type(bytes(s), encoding, errors)

Exception Type: UnicodeEncodeError at /admin/structure/type/add/
Exception Value: 'ascii' codec can't encode character u'\xe9' in position 3: ordinal not in range(128)

编辑:我的python版本是2.7.13,我的django版本是1.8

2 个答案:

答案 0 :(得分:0)

如果使用Python 2,则应使用u修饰符编写Unicode字符串:

print(u"An Unicode string")
print("An ascii string")

答案 1 :(得分:-1)

添加设置文件

 LANG="en_US.utf8", LC_ALL="en_US.UTF-8" ,LC_LANG="en_US.UTF-8" 
 export LANG="en_US.utf8", LC_ALL="en_US.UTF-8" , LC_LANG="en_US.UTF-8" 

在启动django服务器之前,在shell中