Django 模型使用非 ascii verbose_name 时,manage.py 同步数据库错误
我对 Django 还很陌生。
我希望我的模型的名称以中文显示,所以我在模型的元类中使用了 verbose_name,代码如下:
#this models.py file is encoded in unicode
class TS_zone(models.Model):
index = models.IntegerField()
zone_name = models.CharField(max_length=50);
zone_icon = models.ImageField(upload_to='zone_icon', null=True)
is_active = models.NullBooleanField(blank=True, null=True)
status = models.CharField(max_length=7,choices=SETTING_STATUS_CHOICES)
class Meta:
ordering = ('index',)
verbose_name = u'你好嗎?'
verbose_name_plural = u'你們都好嗎?'
def __unicode__(self):
return self.zone_name
但是,当我运行 manage.pysyncdb 时,会抛出以下错误:
File "E:\pythonroot\myproject\..\myproject\myapp\models.py", line 19
SyntaxError: Non-ASCII character '\xe4' in file
E:\pythonroot\myproject\..\myproject\myapp\models.py on line 19, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
看来,manage.py 无法处理我的 verbose_name 中的非 ASCII 字符。我做错了什么吗?
谢谢。
I am pretty new to Django.
I want the name of my models to be displayed in Chinese, so i used verbose_name in my meta class of my model, codes below:
#this models.py file is encoded in unicode
class TS_zone(models.Model):
index = models.IntegerField()
zone_name = models.CharField(max_length=50);
zone_icon = models.ImageField(upload_to='zone_icon', null=True)
is_active = models.NullBooleanField(blank=True, null=True)
status = models.CharField(max_length=7,choices=SETTING_STATUS_CHOICES)
class Meta:
ordering = ('index',)
verbose_name = u'你好嗎?'
verbose_name_plural = u'你們都好嗎?'
def __unicode__(self):
return self.zone_name
However when i run manage.py syncdb, the following errors throws:
File "E:\pythonroot\myproject\..\myproject\myapp\models.py", line 19
SyntaxError: Non-ASCII character '\xe4' in file
E:\pythonroot\myproject\..\myproject\myapp\models.py on line 19, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
It seems that manage.py cannot process non-ascii character in my verbose_name. Anything i have done wrong?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须指定编码。添加以下行作为
models.py
文件的第一行。更新
OP编辑了他的问题,说“
models.py
是用Unicode编码的”。那么这个错误就很奇怪了。它适合我在 Ubuntu Jaunty 上使用 Django 1.2.1、Python 2.6.2。更新 2
您可以发布您用于
models.py
的编码字符串吗?You have to specify an encoding. Add the following line as the first line of your
models.py
file.Update
The OP has edited his question to say that the "
models.py
is encoded in Unicode". Then the error is strange. It works for me using Django 1.2.1, Python 2.6.2 on Ubuntu Jaunty.Update 2
Can you post the encoding string you have used for your
models.py
?