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
谢谢。绝对有帮助! –