Django问题:UnicodeEncodeError

Ess*_*sex 2 python django ascii utf-8 python-2.7

我在我的代码中遇到了这个常见错误:

Exception Type: UnicodeEncodeError
Exception Value:'ascii' codec can't encode character u'\xe9' in position 6: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

为什么?因为我正在处理带有法语口音的名字.

这是我的代码:

if 'rechercheGED' in request.GET:

        query_social_number = request.GET.get('q1social')

        sort_params = {}

        Individu_Recherche.set_if_not_none(sort_params, 'NumeroIdentification__iexact', query_social_number)

        query_lastname_list = Individu_Recherche.Recherche_Get(Individu, sort_params)

        lastname = query_lastname_list.Nom
        firstname = query_lastname_list.Prenom
        NIU = query_lastname_list.NumeroIdentification

        title = str(lastname + "_" + firstname + "_" + NIU)
Run Code Online (Sandbox Code Playgroud)

问题来自: firstname = query_lastname_list.Prenom

因为在我的情况下,名字是 Jérôme

我尝试了一些东西:

1)在开头插入:

#-*- coding: utf-8 -*-
from __future__ import unicode_literals
Run Code Online (Sandbox Code Playgroud)

2)使用firstname = query_lastname_list.Prenom.encode('utf-8')firstname = query_lastname_list.Prenom.decode('utf-8')

但到目前为止,不可能删除此错误并使用重音处理数据.

你有什么主意吗 ?

编辑:

这是完整的Traceback:

Environment:


Request Method: GET
Request URL: http://localhost:8000/Identification/Person/Research/?q1social=19910-00001-634239-2&rechercheGED=Rechercher

Django Version: 1.10.3
Python Version: 2.7.12
Installed Applications:
['Institution',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'bootstrapform',
 'django_countries',
 'debug_toolbar',
 'chartit',
 'Configurations',
 'Home',
 'Authentication',
 'Identity',
 'rest_framework']
Installed Middleware:
['django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.middleware.gzip.GZipMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware',
 'DatasystemsCORE.middleware.OnlineNowMiddleware']



Traceback:

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/Users/valentinjungbluth/Desktop/Django/DatasystemsCORE/DatasystemsCore/DatasystemsCORE/Identity/views.py" in IdentityIndividuResearching
  454.         title = str(lastname + "_" + firstname + "_" + NIU)

Exception Type: UnicodeEncodeError at /Identification/Person/Research/
Exception Value: 'ascii' codec can't encode character u'\xe9' in position 6: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

AKX*_*AKX 5

由于您使用的是Python 2.x,问题确实存在于回溯的最后一行.

title = unicode(str(lastname + "_" + firstname + "_" + NIU))
Run Code Online (Sandbox Code Playgroud)

要么是lastname,firstname要么NIU包含不能用7位ASCII表示的字符,这就是str(在Python 2中).

Django带有有用的函数force_textforce_bytes这些类型的字符串转换,并且使用字符串插值而不是+在做这种事情时是个好主意:

from django.utils.text import force_text

title = force_text('%s_%s_%s' % (lastname, firstname, NIU))
Run Code Online (Sandbox Code Playgroud)