今天学习Django,再创建生成Model后,在管理界面点击add添加中文数据,出现如下问题:

 

Exception Type: UnicodeEncodeError
Exception Value:
'ascii' codec can't encode characters in position 4-7: ordinal not in range(128)

 

Model如下:

class List(models.Model):
    title = models.CharField(max_length=250, unique=True)
    def __str__(self):
        return self.title class Meta:
        ordering = ['title'] 

是__str__()的问题,试添加:

def __unicode__(self):
    return self.title

model如下:

class List(models.Model):
    title = models.CharField(max_length=250, unique=True)
    def __str__(self):
        return self.title 

    def __unicode__(self):
        return self.title 

    class Meta:
        ordering = ['title']

添加数据成功。

相关文章:

  • 2021-06-27
  • 2021-06-19
  • 2022-02-02
  • 2022-01-02
  • 2021-12-29
  • 2021-07-19
  • 2022-12-23
  • 2022-01-02
猜你喜欢
  • 2021-10-31
  • 2022-12-23
  • 2021-08-13
  • 2022-01-06
  • 2022-02-28
  • 2022-02-16
相关资源
相似解决方案