manage.py syncdb error while Django model using non-ascii verbose_name

懵懂的女人 提交于 2019-12-11 13:39:40

问题


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.


回答1:


You have to specify an encoding. Add the following line as the first line of your models.py file.

# encoding: utf-8

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?



来源:https://stackoverflow.com/questions/3656119/manage-py-syncdb-error-while-django-model-using-non-ascii-verbose-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!