UnicodeEncodeError at /admin/shop/product/add/
'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/shop/product/add/
Django Version: 1.9.4
Exception Type: UnicodeEncodeError
Exception Value:
'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
Exception Location: /Library/Python/2.7/site-packages/django/utils/encoding.py in force_text, line 80
Python Executable: /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python Version: 2.7.6
Python Path:
['/Users/similarface/PycharmProjects/mybook',
'/Library/Python/2.7/site-packages/beautifulsoup4-4.4.0-py2.7.egg',
'/Library/Python/2.7/site-packages/html5lib-1.0b7-py2.7.egg',
'/Library/Python/2.7/site-packages/requests-2.7.0-py2.7.egg',
'/Library/Python/2.7/site-packages/Scrapy-1.0.3-py2.7.egg',
'/Library/Python/2.7/site-packages/service_identity-14.0.0-py2.7.egg',
'/Library/Python/2.7/site-packages/six-1.9.0-py2.7.egg',
'/Library/Python/2.7/site-packages/cssselect-0.9.1-py2.7.egg',
'/Library/Python/2.7/site-packages/lxml-3.4.4-py2.7-macosx-10.10-intel.egg',
'/Library/Python/2.7/site-packages/queuelib-1.3.0-py2.7.egg',
'/Library/Python/2.7/site-packages/w3lib-1.12.0-py2.7.egg',
'/Library/Python/2.7/site-packages/pyasn1_modules-0.0.7-py2.7.egg',
'/Library/Python/2.7/site-packages/pyasn1-0.1.8-py2.7.egg',
'/Library/Python/2.7/site-packages/characteristic-14.3.0-py2.7.egg',
'/Library/Python/2.7/site-packages/image-1.4.1-py2.7.egg',
'/Library/Python/2.7/site-packages/Pillow-2.9.0-py2.7-macosx-10.10-intel.egg',
'/Library/Python/2.7/site-packages/PIL-1.1.7-py2.7-macosx-10.10-intel.egg',
'/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg',
'/Library/Python/2.7/site-packages/tornado-4.3-py2.7-macosx-10.10-intel.egg',
'/Users/similarface/PycharmProjects/mybook',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages']
解决办法:
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class Product(models.Model):
category=models.ForeignKey(Category,related_name='products',verbose_name='分类')
name=models.CharField(max_length=200,db_index=True,verbose_name='名称')
slug=models.SlugField(max_length=200,db_index=True,verbose_name='简称')
@python_2_unicode_compatible