Logo

dev-resources.site

for different kinds of informations.

How to use Emojis in a Python + Django + MySQL project

Published at
5/30/2023
Categories
python
django
mysql
emoji
Author
fabiocaccamo
Categories
4 categories in total
python
open
django
open
mysql
open
emoji
open
Author
12 person written this
fabiocaccamo
open
How to use Emojis in a Python + Django + MySQL project

Did you ever received an error like this while saving some text containing Emojis to the database?

DataError at /admin/myapp/mymodel/1/change/
(1366, "Incorrect string value: '\\xF0\\x9F\\x99...' for column 'text' at row 1")
Enter fullscreen mode Exit fullscreen mode

To overcome this problem you have to change the database and application charset to utf8mb4:

MySQL

  • Backup your database

  • Edit MySQL conf and add/set the charset settings:

nano /etc/mysql/mysql.conf.d/mysqld.cnf
Enter fullscreen mode Exit fullscreen mode
#
# * Character Set
#
character_set_server = utf8mb4
collation_server = utf8mb4_unicode_ci
Enter fullscreen mode Exit fullscreen mode
  • Update database tables character-set to utf8mb4:
SELECT CONCAT('ALTER TABLE ', table_name, ' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;') AS alter_statement
FROM information_schema.tables
WHERE table_schema = 'mydatabasename';
Enter fullscreen mode Exit fullscreen mode
  • Run the generated ALTER statements

  • Restart MySQL:

/etc/init.d/mysql restart
Enter fullscreen mode Exit fullscreen mode

Django

  • Add the charset option with utf8mb4 value to your database settings:
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        # ...
        "OPTIONS": {
            "charset": "utf8mb4",
        },
    },
}
Enter fullscreen mode Exit fullscreen mode
  • Restart your application server

Done, now you can store Emojis in your database! 🚀

emoji Article's
30 articles in total
Favicon
Boost Communication on Slack, Discord, GitHub, and Beyond! /BUTIFULL EMOJIS
Favicon
🍳 Emoji Kitchen Game: Create Fun Emoji Combos! (Open Source Project) 🚀
Favicon
Unicode, Emojis, and a bit of Golang
Favicon
🤪 Emojify Text Bookmarklet
Favicon
My First NuGet Package: EmojiToText
Favicon
New Emojis
Favicon
Diversifying Emojis With AI
Favicon
Emojify
Favicon
Biblioteca emoji-picker-element em português
Favicon
How to apply a skin tone to an emoji? 🧛🧛🏻🧛🏼🧛🏽🧛🏾🧛🏿
Favicon
A simple emojis todo app built with Next.js
Favicon
Design Custom Cart Emoji Online with Simplified Emoji Maker
Favicon
Tailwindcss version of Looms's beautiful Emoji Toolbar React component
Favicon
Code Meets Creativity: Crafting Emoji Names with Python
Favicon
Animated emojis for React apps
Favicon
Seamless emoji creation on Discord with bots and Vercel
Favicon
En Popüler Emoji Anlamları ve Kullanımı
Favicon
Fluent UI and Emoji used in Microsoft Teams
Favicon
EventBridge Emoji Event Patterns
Favicon
The Ultimate Emoji Cheat Sheet for Developers
Favicon
Emoji string lengths
Favicon
#LearnedToday: Compound Emoji 👨‍💻
Favicon
How to use Emojis in a Python + Django + MySQL project
Favicon
How to handle emojis in Nodejs
Favicon
Turbocharge Your Rocket.Chat with Bulk Emoji Uploads: Introducing the Ultimate Emoji Management Tool
Favicon
Length of a string
Favicon
Emojis in Development?
Favicon
Create a emoji-picker in dmenu
Favicon
st - the suckless simple terminal, v0.9
Favicon
What are your favourite emoji to use in build pipelines?

Featured ones: