当Django模型使用非ascii verbose_name时,manage.py syncdb错误 [英] manage.py syncdb error while Django model using non-ascii verbose_name

查看:152
本文介绍了当Django模型使用非ascii verbose_name时,manage.py syncdb错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想使用中文显示我的模型名称,所以我在我的模型的meta类中使用了verbose_name,代码如下:

  #this models.py文件编码为unicode 

class TS_zone(models。模型):
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,选择= SETTING_STATUS_CHOICES)
class Meta:
ordering =('index',)
verbose_name = u'你好吗?'
verbose_name_plural = u'你们都好吗?'

def __unicode __(self):
return self.zone_name

但是,当我运行manage.py syncdb时,会出现以下错误:

 文件E:\pythonroot\ myproject\..\myproject\myapp\models.py,第19行
SyntaxError:文件
中的非ASCII字符'\xe4'E:\pythonroot\myproject\\第19行上的\\ ..\myproject\myapp\models.py,但没有声明编码;有关详细信息,请参阅http://www.python.org/peps/pep-0263.html

它似乎manage.py无法处理我的verbose_name中的非ASCII字符。任何我做错了?



谢谢。

解决方案

你必须指定一个编码。添加以下行作为您的 models.py 文件的第一行。

 #encoding:utf-8 

更新



OP已经编辑他的问题,说 models.py 以Unicode编码。那么错误是奇怪的。它适用于我使用Django 1.2.1,Python 2.6.2在Ubuntu Jaunty上。



更新2



您可以发布您为 models.py


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.

解决方案

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?

这篇关于当Django模型使用非ascii verbose_name时,manage.py syncdb错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆