【问题标题】:Not able to add Emoji's as comment "OperationalError :Incorrect string value: '\\xF0\\x9F\\xA5\\xB0' for column 'content' at row 1"无法将表情符号添加为注释“OperationalError :Incorrect string value: '\\xF0\\x9F\\xA5\\xB0' for column 'content' at row 1”
【发布时间】:2020-12-24 21:27:58
【问题描述】:

当我尝试添加表情符号时,我得到了这个错误,即使 CharField 得到同样的错误也没有锻炼。 models.py

class Comment(models.Model):
post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE)
content = models.TextField(max_length=500, blank=False)
author = models.ForeignKey(User, on_delete=models.CASCADE)
date_created = models.DateTimeField(default=timezone.now)

def __str__(self):
    return self.content

def get_absolute_url(self):
    return reverse('post-detail', kwargs={'pk': self.post.pk})  # returns a string to the post detail that uses the pk of the comment instance. post. pk to link to the correct detail page ie. /post/

def save(self, *args, **kwargs):
    super(Comment, self).save(*args, **kwargs)
    n = 4
    truncatewords = Truncator(self.content).words(n)
    notify.send(self.author, recipient=self.post.author, verb='commented "' + truncatewords + '" on your post!', action_object=self.post, description='comment', target=self)

【问题讨论】:

    标签: mysql python-3.x django


    【解决方案1】:

    要允许表情符号字符,您需要将列排序规则设置为 utf8mb4_unicode_ci,这将允许所有字符,并且在 MySQL 连接中,您还需要将 charset 设置为 utf8mb4

    【讨论】:

      猜你喜欢
      • 2020-11-13
      • 2017-04-19
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 2013-12-23
      • 2012-11-19
      • 1970-01-01
      相关资源
      最近更新 更多