问题
I'm using django and tastypie for my api. the db is MySQL.
After a few days of fiddling around i managed to store emoji icons in the db using utf8mb4 character set.
When querying the db directly from the console (on a mac), i see the emoji fine, but when pulling them from the api (for example using the browser), the json shows question marks. This leads me to believe the issue is not with the db but with django/tastypie db connection.
How do i go about it?
回答1:
The solution is in DJango settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {'charset': 'utf8mb4'},
(...)
回答2:
What if you save the JSON dump to a text file and open it with a UTF8 text editor? Will the emoji re-appear?
I suspect that, when displaying the JSON in the browser, the browser falls back to Latin-1 encoding, thus ending up with question marks. There might be a setting in your browser to set the encoding.
Check the "Content-Type" header that comes from your server. Does it say application/json; encoding=utf-8
?
来源:https://stackoverflow.com/questions/15178533/django-tastypie-serving-emoji-that-are-stored-in-db