本文介绍了Django& MySQL unicode错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个不正确的字符串值(异常值:不正确的字符串值:'\xEA\xB0\x95\xED\x95\x98 ...'用于列'在Django和MySQL中尝试保存unicode字符串(韩文)时,第1行的object_repr错误)。第一个问题是数据库表中每列的不正确的字符串值错误。但是,我通过更改列排序规则和整体数据库字符集来计算出来。

I am getting an Incorrect string value (Exception Value: Incorrect string value: '\xEA\xB0\x95\xED\x95\x98...' for column 'object_repr' at row 1) error while trying to save unicode string (Korean) in Django and MySQL.First problem I had was "Incorrect string value" error for each column in the database table. However, I figured this out by changing column collation and overall database character set.

我得到的新错误与models.py.My models.py中的 unicode (self)方法相关如下:

The new error I am getting is related to unicode(self) method in models.py.My models.py is as the following:

from django.db import models

# Create your models here.
class User(models.Model):
full_name = models.CharField(max_length=60)
email = models.EmailField(unique=True)
password = models.CharField(max_length=128)
birthday = models.DateField(null=True, blank=True)
gender = models.PositiveIntegerField(null=True, blank=True)
location = models.CharField(max_length=60, null=True, blank=True)
captcha = models.CharField(max_length=60, null=True, blank=True)

register_date = models.DateTimeField()
lastLogin_date = models.DateTimeField(null=True)
num_logins = models.PositiveIntegerField()

def __unicode__(self):
    return self.full_name

__ unicode __ 函数尝试输出utf8时,会生成错误字符...

The error is generated when the__unicode__ function tries to output utf8 character...

有人知道如何解决这个错误?

Does anybody know how to fix this error?

推荐答案

尝试在这个文件的第一个文本上添加一行:

Try add one line on the first of this file like this:

#-*- encoding=UTF-8 -*-

这篇关于Django& MySQL unicode错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 09:34