Exception Value: 'ascii' codec can't encode character u'\u2013' in position 38: ordinal not in range

来源:互联网 发布:知秋是什么意思 编辑:程序博客网 时间:2024/04/29 06:36

参考http://blog.csdn.net/zniahfag/article/details/51471006?locationNum=7

###########################log start ############################

Django Version: 1.11.7
Python Version: 2.7.6
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'blog',
 'polls',
 'model_study',
 'lava_submission',
 'job_collector',
 'LAVA_job_check']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

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

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

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

File "/var/www/my_django/lava_submission/views.py" in modify_info_iterm
  147.                                    {"project_form":pform.as_ul(), "infoID":info.id}))

File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in as_ul
  294.             errors_on_separate_row=False)

File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in _html_output
  240.                     'field': six.text_type(bf),

File "/usr/local/lib/python2.7/dist-packages/django/utils/html.py" in <lambda>
  376.         klass.__unicode__ = lambda self: mark_safe(klass_unicode(self))

File "/usr/local/lib/python2.7/dist-packages/django/forms/boundfield.py" in __str__
  41.         return self.as_widget()

File "/usr/local/lib/python2.7/dist-packages/django/forms/boundfield.py" in as_widget
  127.             **kwargs

File "/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py" in render
  220.         context = self.get_context(name, value, attrs)

File "/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py" in get_context
  665.         context = super(Select, self).get_context(name, value, attrs)

File "/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py" in get_context
  627.         context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs)

File "/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py" in optgroups
  575.         for index, (option_value, option_label) in enumerate(chain(self.choices)):

File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in __iter__
  1140.             yield self.choice(obj)

File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in choice
  1146.         return (self.field.prepare_value(obj), self.field.label_from_instance(obj))

File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in label_from_instance
  1211.         return force_text(obj)

File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py" in force_text
  80.                 s = six.text_type(bytes(s), encoding, errors)

Exception Type: UnicodeEncodeError at /lava_submit/modifyInfo/
Exception Value: 'ascii' codec can't encode character u'\u2013' in position 38: ordinal not in range(128)

Django Version: 1.11.7
Python Version: 2.7.6
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'blog',
 'polls',
 'model_study',
 'lava_submission',
 'job_collector',
 'LAVA_job_check']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

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

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

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

File "/var/www/my_django/lava_submission/views.py" in modify_info_iterm
  147.                                    {"project_form":pform.as_ul(), "infoID":info.id}))

File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in as_ul
  294.             errors_on_separate_row=False)

File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in _html_output
  240.                     'field': six.text_type(bf),

File "/usr/local/lib/python2.7/dist-packages/django/utils/html.py" in <lambda>
  376.         klass.__unicode__ = lambda self: mark_safe(klass_unicode(self))

File "/usr/local/lib/python2.7/dist-packages/django/forms/boundfield.py" in __str__
  41.         return self.as_widget()

File "/usr/local/lib/python2.7/dist-packages/django/forms/boundfield.py" in as_widget
  127.             **kwargs

File "/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py" in render
  220.         context = self.get_context(name, value, attrs)

File "/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py" in get_context
  665.         context = super(Select, self).get_context(name, value, attrs)

File "/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py" in get_context
  627.         context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs)

File "/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py" in optgroups
  575.         for index, (option_value, option_label) in enumerate(chain(self.choices)):

File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in __iter__
  1140.             yield self.choice(obj)

File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in choice
  1146.         return (self.field.prepare_value(obj), self.field.label_from_instance(obj))

File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in label_from_instance
  1211.         return force_text(obj)

File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py" in force_text
  80.                 s = six.text_type(bytes(s), encoding, errors)

Exception Type: UnicodeEncodeError at /lava_submit/modifyInfo/
Exception Value: 'ascii' codec can't encode character u'\u2013' in position 38: ordinal not in range(128)

###########################log end############################


view.py
def AddProjectInfo(request):
    if request.method == "POST":
        web_infos = get_branch_project_info_from_jenkins()
        db_save_branch_project_infos(web_infos)
        info = VerifyProjectInfo.objects.all()
        if info.count() != 0:
            pform = ProjectInfoFrom(instance=info.first())
        else:
            pform = ProjectInfoFrom(request.POST)
        pform = ProjectInfoFrom(request.POST)
        return HttpResponse(render(request, 'lava_submission/add_project_info.html', {"project_form":pform.as_ul()}))

    else:
        raise Http404('Method not POST')


###########################解决方法 ############################

在view.py中添加

import sys
reload(sys)
sys.setdefaultencoding("utf8")

阅读全文
0 0
原创粉丝点击