unicode和字符串編碼仍然讓我頭疼。 I follow this question/answer to can be added special characters(äÄÜ..)to message。Django unicode concatenation
對於下面的結構,我很難理解爲什麼版本2工作,版本1沒有。
我的模型:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
class Project(models.Model):
"""
Representation of a project
"""
name = models.CharField(max_length=200)
def __unicode__(self):
return '%s ' % (self.name)
版本1:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
def print_project(self, project):
project_prefix = "Project: "
print (project_prefix + str(project))
版本2:
# -*- coding: utf-8 -*-
def print_project(self, project):
project_prefix = "Project: "
print (project_prefix + str(project))
正如你看到的,唯一的區別是,我做這個from __future__ import unicode_literals
進口。拋出的錯誤如下:
'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
這應該有用嗎? http://stackoverflow.com/questions/809796/any-gotchas-using-unicode-literals-in-python-2-6 – karthikr
謝謝。絕對有幫助! –